Implemented setup of the admiralty env on two peers in a workflow before execution
This commit is contained in:
		| @@ -1,14 +1,17 @@ | ||||
| package workflow_builder | ||||
|  | ||||
| import ( | ||||
| 	"encoding/json" | ||||
| 	"fmt" | ||||
| 	"net/http" | ||||
|  | ||||
| 	oclib "cloud.o-forge.io/core/oc-lib" | ||||
| 	"cloud.o-forge.io/core/oc-lib/models/peer" | ||||
| 	"cloud.o-forge.io/core/oc-lib/tools" | ||||
| ) | ||||
|  | ||||
| type AdmiraltySetter struct { | ||||
| 	Id	string | ||||
| 	Id	string		// ID to identify the execution, correspond to workflow_executions id | ||||
| } | ||||
|  | ||||
| func (s *AdmiraltySetter) InitializeAdmiralty(localPeerID string,remotePeerID string) error { | ||||
| @@ -20,34 +23,71 @@ func (s *AdmiraltySetter) InitializeAdmiralty(localPeerID string,remotePeerID st | ||||
| 	} | ||||
| 	remotePeer := data.ToPeer() | ||||
|  | ||||
| 	data = oclib.NewRequest(oclib.LibDataEnum(oclib.PEER),"",localPeerID,nil,nil).LoadOne(localPeerID) | ||||
| 	if data.Code != 200 { | ||||
| 		logger.Error().Msg("Error while trying to instantiate local peer " + remotePeerID) | ||||
| 		return fmt.Errorf(data.Err) | ||||
| 	} | ||||
| 	localPeer := data.ToPeer() | ||||
|  | ||||
| 	caller := tools.NewHTTPCaller( | ||||
| 		map[tools.DataType]map[tools.METHOD]string{ | ||||
| 			tools.ADMIRALTY_SOURCE: map[tools.METHOD]string{ | ||||
| 				tools.POST : "/:id", | ||||
| 				tools.POST :"/:id", | ||||
| 			}, | ||||
| 			tools.ADMIRALTY_KUBECONFIG: map[tools.METHOD]string{ | ||||
| 				tools.POST: "/:id", | ||||
| 				tools.GET:"/:id", | ||||
| 			}, | ||||
| 			tools.ADMIRALTY_SECRET: map[tools.METHOD]string{ | ||||
| 				tools.POST: "/:id", | ||||
| 				tools.POST:"/:id", | ||||
| 			}, | ||||
| 			tools.ADMIRALTY_TARGET: map[tools.METHOD]string{ | ||||
| 				tools.POST: "/:id", | ||||
| 				tools.POST:"/:id", | ||||
| 			}, | ||||
| 			tools.ADMIRALTY_NODES: map[tools.METHOD]string{ | ||||
| 				tools.GET: "/id", | ||||
| 				tools.GET:"/:id", | ||||
| 			}, | ||||
| 		}, | ||||
| 	) | ||||
| 	fmt.Println("Creating source in ") | ||||
| 	resp, err := remotePeer.LaunchPeerExecution(remotePeer.UUID,"toto-5",tools.ADMIRALTY_SOURCE,tools.POST,nil,caller) | ||||
| 	fmt.Println("Creating source in", remotePeerID, " ns-" + s.Id) | ||||
| 	_ = s.callRemoteExecution(remotePeer, http.StatusCreated,caller, s.Id, tools.ADMIRALTY_SOURCE, tools.POST, nil) | ||||
| 	kubeconfig := s.getKubeconfig(remotePeer, caller) | ||||
| 	_ = s.callRemoteExecution(localPeer, http.StatusCreated, caller,s.Id, tools.ADMIRALTY_SECRET, tools.POST,kubeconfig) | ||||
| 	_ = s.callRemoteExecution(localPeer,http.StatusCreated,caller,s.Id,tools.ADMIRALTY_TARGET,tools.POST, nil) | ||||
| 	_ = s.callRemoteExecution(localPeer,http.StatusOK,caller,s.Id,tools.ADMIRALTY_NODES,tools.GET, nil) | ||||
| 	 | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (s *AdmiraltySetter) getKubeconfig(peer *peer.Peer, caller *tools.HTTPCaller) map[string]string { | ||||
| 	var kubedata map[string]string | ||||
| 	_ = s.callRemoteExecution(peer, http.StatusOK, caller, s.Id, tools.ADMIRALTY_KUBECONFIG, tools.GET, nil) | ||||
| 	if caller.LastResults["body"] == nil || len(caller.LastResults["body"].([]byte)) == 0 { | ||||
| 		fmt.Println("Something went wrong when retrieving data from Get call for kubeconfig") | ||||
| 		panic(0) | ||||
| 	} | ||||
| 	err := json.Unmarshal(caller.LastResults["body"].([]byte), &kubedata) | ||||
| 	if err != nil { | ||||
| 		fmt.Println("Error contacting remote peer") | ||||
| 		fmt.Println(err) | ||||
|    		panic(0) | ||||
| 		fmt.Println("Something went wrong when unmarshalling data from Get call for kubeconfig") | ||||
| 		panic(0) | ||||
| 	} | ||||
|  | ||||
| 	fmt.Println(resp) | ||||
| 	return kubedata | ||||
| } | ||||
|  | ||||
| 	return nil | ||||
| func (*AdmiraltySetter) callRemoteExecution(peer *peer.Peer, expectedCode int,caller *tools.HTTPCaller, dataID string, dt tools.DataType, method tools.METHOD, body interface{}) *peer.PeerExecution { | ||||
| 	resp, err := peer.LaunchPeerExecution(peer.UUID, dataID, dt, method, body, caller) | ||||
| 	if err != nil { | ||||
| 		fmt.Println("Error when executing on peer at", peer.Url) | ||||
| 		fmt.Println(err) | ||||
| 		panic(0) | ||||
| 	} | ||||
|  | ||||
| 	if caller.LastResults["code"].(int) != expectedCode { | ||||
| 		fmt.Println("Didn't receive the expected code :", caller.LastResults["code"], "when expecting", expectedCode) | ||||
| 		fmt.Println(string(caller.LastResults["body"].(byte))) | ||||
| 		panic(0) | ||||
| 	} | ||||
|  | ||||
| 	return resp | ||||
| } | ||||
		Reference in New Issue
	
	Block a user