Add support for builds using rescue mode

This commit is contained in:
dedene 2018-11-23 20:35:32 +01:00
parent e539133d8c
commit 064e6c7e08
3 changed files with 62 additions and 1 deletions

View File

@ -35,6 +35,8 @@ type Config struct {
UserDataFile string `mapstructure:"user_data_file"`
SSHKeys []string `mapstructure:"ssh_keys"`
RescueMode string `mapstructure:"rescue"`
ctx interpolate.Context
}

View File

@ -68,11 +68,12 @@ func (s *stepCreateServer) Run(ctx context.Context, state multistep.StateBag) mu
state.Put("server_id", serverCreateResult.Server.ID)
_, errCh := client.Action.WatchProgress(context.TODO(), serverCreateResult.Action)
watch:
for {
select {
case err1 := <-errCh:
if err1 == nil {
return multistep.ActionContinue
break watch
} else {
err := fmt.Errorf("Error creating server: %s", err)
state.Put("error", err)
@ -82,6 +83,17 @@ func (s *stepCreateServer) Run(ctx context.Context, state multistep.StateBag) mu
}
}
if c.RescueMode != "" {
if err := setRescue(ctx, client, serverCreateResult.Server, c.RescueMode, sshKeys); err != nil {
err := fmt.Errorf("Error enabling rescue mode: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
}
return multistep.ActionContinue
}
func (s *stepCreateServer) Cleanup(state multistep.StateBag) {
@ -101,3 +113,48 @@ func (s *stepCreateServer) Cleanup(state multistep.StateBag) {
"Error destroying server. Please destroy it manually: %s", err))
}
}
func setRescue(ctx context.Context, client *hcloud.Client, server *hcloud.Server, rescue string, sshKeys []*hcloud.SSHKey) error {
rescueChanged := false
if server.RescueEnabled {
rescueChanged = true
action, _, err := client.Server.DisableRescue(ctx, server)
if err != nil {
return err
}
if err := waitForServerAction(ctx, client, action, server); err != nil {
return err
}
}
if rescue != "" {
rescueChanged = true
res, _, err := client.Server.EnableRescue(ctx, server, hcloud.ServerEnableRescueOpts{
Type: hcloud.ServerRescueType(rescue),
SSHKeys: sshKeys,
})
if err != nil {
return err
}
if err := waitForServerAction(ctx, client, res.Action, server); err != nil {
return err
}
}
if rescueChanged {
action, _, err := client.Server.Reset(ctx, server)
if err != nil {
return err
}
if err := waitForServerAction(ctx, client, action, server); err != nil {
return err
}
}
return nil
}
func waitForServerAction(ctx context.Context, client *hcloud.Client, action *hcloud.Action, server *hcloud.Server) error {
_, errCh := client.Action.WatchProgress(ctx, action)
if err := <-errCh; err != nil {
return err
}
return nil
}

View File

@ -72,6 +72,8 @@ builder.
- `ssh_keys` (array of strings) - List of SSH keys by name or id to be added
to image on launch.
- `rescue` (string) - Enable and boot in to the specified rescue system. This enables simple installation of custom operating systems. `linux64` `linux32` or `freebsd64`
## Basic Example
Here is a basic example. It is completely valid as soon as you enter your own