Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 18 additions & 41 deletions virtualmachine/gcp/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"strings"
"time"
"context"
Expand Down Expand Up @@ -43,11 +41,18 @@ type googleResManService struct {
service *googleresourcemanager.Service
}

// accountFile represents the structure of the account file JSON file.
type accountFile struct {
PrivateKey string `json:"private_key"`
ClientEmail string `json:"client_email"`
ClientId string `json:"client_id"`
// AccountFile represents the structure of the account file JSON file.
type AccountFile struct {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any specific reason to expose the struct ?

AuthProviderX509CertURL string `json:"auth_provider_x509_cert_url"`
AuthURI string `json:"auth_uri"`
ClientEmail string `json:"client_email"`
ClientId string `json:"client_id"`
ClientX509CertURL string `json:"client_x509_cert_url"`
PrivateKey string `json:"private_key"`
PrivateKeyId string `json:"private_key_id"`
ProjectId string `json:"project_id"`
TokenURI string `json:"token_uri"`
Type string `json:"type"`
}

// getResManService processes the account file and returns the service object
Expand All @@ -56,15 +61,11 @@ func (acc *Account) getResManService() (*googleResManService, error) {
var err error
var client *http.Client

if err = parseAccountFile(&acc.account, acc.AccountFile); err != nil {
return nil, err
}

// Auth with AccountFile first if provided
if acc.account.PrivateKey != "" {
if acc.Account.PrivateKey != "" {
config := jwt.Config{
Email: acc.account.ClientEmail,
PrivateKey: []byte(acc.account.PrivateKey),
Email: acc.Account.ClientEmail,
PrivateKey: []byte(acc.Account.PrivateKey),
Scopes: acc.Scopes,
TokenURL: tokenURL,
}
Expand All @@ -90,15 +91,11 @@ func (vm *VM) getService() (*googleService, error) {
var err error
var client *http.Client

if err = parseAccountFile(&vm.account, vm.AccountFile); err != nil {
return nil, err
}

// Auth with AccountFile first if provided
if vm.account.PrivateKey != "" {
if vm.Account.PrivateKey != "" {
config := jwt.Config{
Email: vm.account.ClientEmail,
PrivateKey: []byte(vm.account.PrivateKey),
Email: vm.Account.ClientEmail,
PrivateKey: []byte(vm.Account.PrivateKey),
Scopes: vm.Scopes,
TokenURL: tokenURL,
}
Expand Down Expand Up @@ -445,26 +442,6 @@ func parseAccountJSON(result interface{}, jsonText string) error {
return dec.Decode(result)
}

func parseAccountFile(file *accountFile, account string) error {
if err := parseAccountJSON(file, account); err != nil {
if _, err = os.Stat(account); os.IsNotExist(err) {
return fmt.Errorf("error finding account file: %s", account)
}

bytes, err := ioutil.ReadFile(account)
if err != nil {
return fmt.Errorf("error reading account file from path '%s': %s", file, err)
}

err = parseAccountJSON(file, string(bytes))
if err != nil {
return fmt.Errorf("error parsing account file: %s", err)
}
}

return nil
}

func (svc *googleService) getSSHKey() string {
return fmt.Sprintf("%s:%s\n", svc.vm.SSHCreds.SSHUser, svc.vm.SSHPublicKey)
}
Expand Down
43 changes: 41 additions & 2 deletions virtualmachine/gcp/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package gcp

import (
"encoding/json"
"errors"
"fmt"
"net"
Expand Down Expand Up @@ -45,6 +46,7 @@ var (
type VM struct {
Name string
Description string
Region string
Zone string
MachineType string
Preemptible bool // Preemptible instances will be terminates after they run for 24 hours.
Expand All @@ -64,7 +66,7 @@ type VM struct {
Tags []string //Instance Tags

AccountFile string
account accountFile
Account AccountFile
SSHCreds ssh.Credentials
SSHPublicKey string

Expand Down Expand Up @@ -206,7 +208,7 @@ type Account struct {
AccountFile string
// account: Represents a structure containing private key, client email
// and client ID parsed from AccountFile
account accountFile
Account AccountFile
// Scopes: Represents access scopes with which API call is made
Scopes []string
}
Expand All @@ -225,11 +227,47 @@ type Project struct {
CreateTime string `json:"create_time,omitempty"`
}

// Packer visor image config for GCP cloud
type GcpConfig struct {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GcpPackerConfig ?

AccountFile string `json:"account_file"`
ProjectID string `json:"project_id"`
SourceImage string `json:"source_image"`
SSHUsername string `json:"ssh_username,omitempty"`
Type string `json:"type"`
Zone string `json:"zone"`
StartupScript string `json:"startup_script_file"`
ImageName string `json:"image_name,omitempty"`
}

// Packer visor image config for GCP cloud
type VisorImageConfig struct {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PackerConfig ?

Builders []GcpConfig `json:"builders"`
}

// GetName returns the name of the virtual machine.
func (vm *VM) GetName() string {
return vm.Name
}

// Creates a visor image config required for packer
func (vm *VM) CreateVisorImageConfig(accountFile, packerConf string) ([]byte, error) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we return only the gcp builder here and merge where we are dumping this in a file (c3ntry) ?

var vig VisorImageConfig
var conf GcpConfig

conf.ImageName = vm.Name
conf.AccountFile = accountFile
conf.Zone = vm.Zone
conf.SourceImage = vm.SourceImage
conf.ProjectID = vm.Project
conf.Type = "googlecompute"
conf.StartupScript = packerConf
conf.SSHUsername = "apporbit"

vig.Builders = append(vig.Builders, conf)

return json.Marshal(vig)
}

// Provision creates a virtual machine on GCE. It returns an error if
// there was a problem during creation.
func (vm *VM) Provision() error {
Expand Down Expand Up @@ -524,6 +562,7 @@ func (vm *VM) GetZoneList() ([]Zone, error) {
Region: regionName,
Status: zone.Status,
})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete extra line.

}

return response, nil
Expand Down