@@ -39,6 +39,7 @@ type Driver struct {
3939 Password string
4040 PublicKey string
4141 UserDataFile string
42+ UserData []byte
4243 ID string `json:"Id"`
4344}
4445
@@ -199,6 +200,7 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
199200 d .SSHUser = flags .String ("exoscale-ssh-user" )
200201 d .SSHKey = flags .String ("exoscale-ssh-key" )
201202 d .UserDataFile = flags .String ("exoscale-userdata" )
203+ d .UserData = []byte (defaultCloudInit )
202204 d .SetSwarmConfigFromFlags (flags )
203205
204206 if d .URL == "" {
@@ -416,47 +418,55 @@ func (d *Driver) Create() error {
416418 zone := zones .Zone [0 ].ID
417419 log .Debugf ("Availability zone %v = %s" , d .AvailabilityZone , zone )
418420
419- // Image UUID
420- var tpl string
421+ // Image
422+ template := egoscale.Template {
423+ IsFeatured : true ,
424+ ZoneID : "1" , // GVA2
425+ }
421426
422- resp , err = client .RequestWithContext (context .TODO (), & egoscale.ListTemplates {
423- TemplateFilter : "featured" ,
424- ZoneID : "1" , // GVA2
425- })
427+ templates , err := client .ListWithContext (context .TODO (), & template )
426428 if err != nil {
427429 return err
428430 }
429431
430432 image := strings .ToLower (d .Image )
431433 re := regexp .MustCompile (`^Linux (?P<name>.+?) (?P<version>[0-9.]+)\b` )
432- for _ , template := range resp .(* egoscale.ListTemplatesResponse ).Template {
434+
435+ for _ , t := range templates {
436+ tpl := t .(* egoscale.Template )
437+
433438 // Keep only 10GiB images
434- if template .Size >> 30 != 10 {
439+ if tpl .Size >> 30 != 10 {
435440 continue
436441 }
437442
438- fullname := strings .ToLower (template .Name )
443+ fullname := strings .ToLower (tpl .Name )
439444 if image == fullname {
440- tpl = template . ID
445+ template = * tpl
441446 break
442447 }
443448
444- submatch := re .FindStringSubmatch (template .Name )
449+ submatch := re .FindStringSubmatch (tpl .Name )
445450 if len (submatch ) > 0 {
446451 name := strings .Replace (strings .ToLower (submatch [1 ]), " " , "-" , - 1 )
447452 version := submatch [2 ]
448453 shortname := fmt .Sprintf ("%s-%s" , name , version )
449454
450455 if image == shortname {
451- tpl = template . ID
456+ template = * tpl
452457 break
453458 }
454459 }
455460 }
456- if tpl == "" {
461+ if template . ID == "" {
457462 return fmt .Errorf ("Unable to find image %v" , d .Image )
458463 }
459- log .Debugf ("Image %v(10) = %s" , d .Image , tpl )
464+
465+ // Reading the username from the template
466+ if name , ok := template .Details ["username" ]; ok {
467+ d .SSHUser = name
468+ }
469+ log .Debugf ("Image %v(10) = %s (%s)" , d .Image , template .ID , d .SSHUser )
460470
461471 // Profile UUID
462472 resp , err = client .RequestWithContext (context .TODO (), & egoscale.ListServiceOfferings {
@@ -531,8 +541,11 @@ func (d *Driver) Create() error {
531541 return fmt .Errorf ("SSH Key pair creation failed %s" , err )
532542 }
533543 keyPair := resp .(* egoscale.CreateSSHKeyPairResponse ).KeyPair
544+ if err = os .MkdirAll (filepath .Dir (d .GetSSHKeyPath ()), 0750 ); err != nil {
545+ return fmt .Errorf ("Cannot create the folder to store the SSH private key. %s" , err )
546+ }
534547 if err = ioutil .WriteFile (d .GetSSHKeyPath (), []byte (keyPair .PrivateKey ), 0600 ); err != nil {
535- return fmt .Errorf ("SSH public key could not be written %s" , err )
548+ return fmt .Errorf ("SSH private key could not be written. %s" , err )
536549 }
537550 d .KeyPair = keyPairName
538551 } else {
@@ -574,12 +587,13 @@ ssh_authorized_keys:
574587 log .Debugf ("%s" , string (cloudInit ))
575588
576589 // Base64 encode the userdata
577- userData := base64 .StdEncoding .EncodeToString (cloudInit )
590+ d .UserData = cloudInit
591+ encodedUserData := base64 .StdEncoding .EncodeToString (d .UserData )
578592
579593 req := & egoscale.DeployVirtualMachine {
580- TemplateID : tpl ,
594+ TemplateID : template . ID ,
581595 ServiceOfferingID : profile ,
582- UserData : userData ,
596+ UserData : encodedUserData ,
583597 ZoneID : zone ,
584598 Name : d .MachineName ,
585599 KeyPair : d .KeyPair ,
@@ -688,9 +702,10 @@ func (d *Driver) Remove() error {
688702// Build a cloud-init user data string that will install and run
689703// docker.
690704func (d * Driver ) getCloudInit () ([]byte , error ) {
705+ var err error
691706 if d .UserDataFile != "" {
692- return ioutil .ReadFile (d .UserDataFile )
707+ d . UserData , err = ioutil .ReadFile (d .UserDataFile )
693708 }
694709
695- return [] byte ( defaultCloudInit ), nil
710+ return d . UserData , err
696711}
0 commit comments