Skip to content

Commit

Permalink
Merge pull request #36 from gfieni/feat-stateless-api-client
Browse files Browse the repository at this point in the history
Stateless API client
  • Loading branch information
gfieni authored Nov 5, 2019
2 parents 7483d35 + 74acbaa commit 0d8936e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
31 changes: 24 additions & 7 deletions driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const g5kReferenceEnvironmentName string = "debian10-x64-std"
type Driver struct {
*drivers.BaseDriver

G5kAPI *api.Client
// Persistent fields
G5kJobID int
G5kUsername string
G5kPassword string
Expand All @@ -38,6 +38,9 @@ type Driver struct {
ExternalSSHPublicKeys []string
G5kKeepAllocatedResourceAtDeletion bool
G5kNodeHostname string

// Ephemeral fields
g5kAPI *api.Client
}

// NewDriver creates and returns a new instance of the driver
Expand Down Expand Up @@ -203,7 +206,9 @@ func (d *Driver) SetConfigFromFlags(opts drivers.DriverOptions) error {
func (d *Driver) GetIP() (string, error) {
if d.IPAddress == "" {
if d.G5kNodeHostname == "" {
job, err := d.G5kAPI.GetJob(d.G5kJobID)
d.g5kAPI = api.NewClient(d.G5kUsername, d.G5kPassword, d.G5kSite)

job, err := d.g5kAPI.GetJob(d.G5kJobID)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -267,7 +272,9 @@ func (d *Driver) GetURL() (string, error) {

// GetState returns the state that the host is in (running, stopped, etc)
func (d *Driver) GetState() (state.State, error) {
job, err := d.G5kAPI.GetJob(d.G5kJobID)
d.g5kAPI = api.NewClient(d.G5kUsername, d.G5kPassword, d.G5kSite)

job, err := d.g5kAPI.GetJob(d.G5kJobID)
if err != nil {
return state.None, err
}
Expand Down Expand Up @@ -320,10 +327,8 @@ func (d *Driver) PreCreateCheck() error {
return err
}

// create API client
d.G5kAPI = api.NewClient(d.G5kUsername, d.G5kPassword, d.G5kSite)
d.g5kAPI = api.NewClient(d.G5kUsername, d.G5kPassword, d.G5kSite)

// load driver SSH public key
if err := d.loadDriverSSHPublicKey(); err != nil {
return err
}
Expand Down Expand Up @@ -359,6 +364,8 @@ func (d *Driver) PreCreateCheck() error {

// Create wait for the job to be running, deploy the OS image and copy the ssh keys
func (d *Driver) Create() error {
d.g5kAPI = api.NewClient(d.G5kUsername, d.G5kPassword, d.G5kSite)

// wait for job to be in 'running' state
if err := d.waitUntilJobIsReady(); err != nil {
return err
Expand All @@ -381,31 +388,41 @@ func (d *Driver) Create() error {

// Remove delete the resources reservation
func (d *Driver) Remove() error {
d.g5kAPI = api.NewClient(d.G5kUsername, d.G5kPassword, d.G5kSite)

// keep the resource allocated if the user asked for it
if !d.G5kKeepAllocatedResourceAtDeletion {
log.Infof("Deallocating resource... (Job ID: '%d')", d.G5kJobID)
return d.G5kAPI.KillJob(d.G5kJobID)
return d.g5kAPI.KillJob(d.G5kJobID)
}

return nil
}

// Kill perform a hard power-off on the node
func (d *Driver) Kill() error {
d.g5kAPI = api.NewClient(d.G5kUsername, d.G5kPassword, d.G5kSite)

return d.changeNodePowerStatus("off", "hard")
}

// Start perform a soft power-on on the node
func (d *Driver) Start() error {
d.g5kAPI = api.NewClient(d.G5kUsername, d.G5kPassword, d.G5kSite)

return d.changeNodePowerStatus("on", "soft")
}

// Stop perform a soft power-off on the node
func (d *Driver) Stop() error {
d.g5kAPI = api.NewClient(d.G5kUsername, d.G5kPassword, d.G5kSite)

return d.changeNodePowerStatus("off", "soft")
}

// Restart perform a soft reboot on the node
func (d *Driver) Restart() error {
d.g5kAPI = api.NewClient(d.G5kUsername, d.G5kPassword, d.G5kSite)

return d.rebootNode("soft")
}
20 changes: 10 additions & 10 deletions driver/g5k.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (d *Driver) waitUntilJobIsReady() error {
log.Info("Waiting for job to run...")

// refresh job state
for job, err := d.G5kAPI.GetJob(d.G5kJobID); job.State != "running"; job, err = d.G5kAPI.GetJob(d.G5kJobID) {
for job, err := d.g5kAPI.GetJob(d.G5kJobID); job.State != "running"; job, err = d.g5kAPI.GetJob(d.G5kJobID) {
// check if GetJob returned an error
if err != nil {
return err
Expand Down Expand Up @@ -66,7 +66,7 @@ func (d *Driver) makeJobSubmission() error {
}

// submit new Job request
jobID, err := d.G5kAPI.SubmitJob(api.JobRequest{
jobID, err := d.g5kAPI.SubmitJob(api.JobRequest{
Resources: fmt.Sprintf("nodes=1,walltime=%s", d.G5kWalltime),
Command: jobCommand,
Properties: d.G5kResourceProperties,
Expand All @@ -88,7 +88,7 @@ func (d *Driver) makeJobReservation() error {
jobTypes := []string{"deploy"}

// submit new Job request
jobID, err := d.G5kAPI.SubmitJob(api.JobRequest{
jobID, err := d.g5kAPI.SubmitJob(api.JobRequest{
Resources: fmt.Sprintf("nodes=1,walltime=%s", d.G5kWalltime),
Command: jobCommand,
Properties: d.G5kResourceProperties,
Expand All @@ -111,7 +111,7 @@ func (d *Driver) waitUntilWorkflowIsDone(operation string, wid string, node stri

for {
// get operation workflow
workflow, err := d.G5kAPI.GetOperationWorkflow(operation, wid)
workflow, err := d.g5kAPI.GetOperationWorkflow(operation, wid)
if err != nil {
return err
}
Expand Down Expand Up @@ -148,7 +148,7 @@ func (d *Driver) deployImageToNode() error {
}

// get job informations
job, err := d.G5kAPI.GetJob(d.G5kJobID)
job, err := d.g5kAPI.GetJob(d.G5kJobID)
if err != nil {
return fmt.Errorf("Error when getting job (id: '%d') informations: %s", d.G5kJobID, err.Error())
}
Expand All @@ -175,7 +175,7 @@ func (d *Driver) deployImageToNode() error {
sshAuthorizedKeysBase64 := base64.StdEncoding.EncodeToString([]byte(GenerateSSHAuthorizedKeys(d.DriverSSHPublicKey, d.ExternalSSHPublicKeys)))

// submit deployment operation to kadeploy
op, err := d.G5kAPI.SubmitDeployment(api.DeploymentOperation{
op, err := d.g5kAPI.SubmitDeployment(api.DeploymentOperation{
Nodes: []string{node},
Environment: api.DeploymentOperationEnvironment{
Kind: "database",
Expand Down Expand Up @@ -217,7 +217,7 @@ func (d *Driver) getNodePowerState() (string, error) {
return "", fmt.Errorf("Failed to get the node hostname: %s", err.Error())
}

op, err := d.G5kAPI.RequestPowerStatus(node)
op, err := d.g5kAPI.RequestPowerStatus(node)
if err != nil {
return "", fmt.Errorf("Failed to request power status: %s", err.Error())
}
Expand All @@ -227,7 +227,7 @@ func (d *Driver) getNodePowerState() (string, error) {
}

// get nodes states for the workflow
states, err := d.G5kAPI.GetOperationStates("power", op.WID)
states, err := d.g5kAPI.GetOperationStates("power", op.WID)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -259,7 +259,7 @@ func (d *Driver) changeNodePowerStatus(status string, level string) error {
return fmt.Errorf("Failed to get the node hostname: %s", err.Error())
}

op, err := d.G5kAPI.SubmitPowerOperation(api.PowerOperation{
op, err := d.g5kAPI.SubmitPowerOperation(api.PowerOperation{
Nodes: []string{node},
Status: status,
Level: level,
Expand All @@ -284,7 +284,7 @@ func (d *Driver) rebootNode(level string) error {
return fmt.Errorf("Failed to get the node hostname: %s", err.Error())
}

op, err := d.G5kAPI.SubmitRebootOperation(api.RebootOperation{
op, err := d.g5kAPI.SubmitRebootOperation(api.RebootOperation{
Kind: "simple",
Nodes: []string{node},
Level: level,
Expand Down

0 comments on commit 0d8936e

Please sign in to comment.