~ubuntu-branches/ubuntu/trusty/juju-core/trusty-proposed

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/provider/null/environ.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-01-29 11:40:20 UTC
  • mfrom: (23.1.1 trusty-proposed)
  • Revision ID: package-import@ubuntu.com-20140129114020-ejieitm8smtt5vln
Tags: 1.17.1-0ubuntu2
d/tests/local-provider: Don't fail tests if ~/.juju is present as its
created by the juju version command. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
package null
5
5
 
6
6
import (
 
7
        "bytes"
7
8
        "errors"
 
9
        "fmt"
8
10
        "net"
9
11
        "path"
 
12
        "strings"
10
13
        "sync"
11
14
 
12
15
        "launchpad.net/loggo"
26
29
        "launchpad.net/juju-core/state"
27
30
        "launchpad.net/juju-core/state/api"
28
31
        "launchpad.net/juju-core/tools"
 
32
        "launchpad.net/juju-core/utils/ssh"
29
33
        "launchpad.net/juju-core/worker/localstorage"
 
34
        "launchpad.net/juju-core/worker/terminationworker"
30
35
)
31
36
 
32
37
const (
48
53
type nullEnviron struct {
49
54
        cfg                   *environConfig
50
55
        cfgmutex              sync.Mutex
51
 
        bootstrapStorage      *sshstorage.SSHStorage
 
56
        bootstrapStorage      storage.Storage
52
57
        bootstrapStorageMutex sync.Mutex
 
58
        ubuntuUserInited      bool
 
59
        ubuntuUserInitMutex   sync.Mutex
53
60
}
54
61
 
55
62
var _ environs.BootstrapStorager = (*nullEnviron)(nil)
85
92
        return e.envConfig().Name()
86
93
}
87
94
 
 
95
var initUbuntuUser = manual.InitUbuntuUser
 
96
 
 
97
func (e *nullEnviron) ensureBootstrapUbuntuUser(ctx environs.BootstrapContext) error {
 
98
        e.ubuntuUserInitMutex.Lock()
 
99
        defer e.ubuntuUserInitMutex.Unlock()
 
100
        if e.ubuntuUserInited {
 
101
                return nil
 
102
        }
 
103
        cfg := e.envConfig()
 
104
        err := initUbuntuUser(cfg.bootstrapHost(), cfg.bootstrapUser(), cfg.AuthorizedKeys(), ctx.Stdin(), ctx.Stdout())
 
105
        if err != nil {
 
106
                logger.Errorf("initializing ubuntu user: %v", err)
 
107
                return err
 
108
        }
 
109
        logger.Infof("initialized ubuntu user")
 
110
        e.ubuntuUserInited = true
 
111
        return nil
 
112
}
 
113
 
88
114
func (e *nullEnviron) Bootstrap(ctx environs.BootstrapContext, cons constraints.Value) error {
 
115
        if err := e.ensureBootstrapUbuntuUser(ctx); err != nil {
 
116
                return err
 
117
        }
89
118
        envConfig := e.envConfig()
90
 
        hc, series, err := manual.DetectSeriesAndHardwareCharacteristics(envConfig.sshHost())
 
119
        host := envConfig.bootstrapHost()
 
120
        hc, series, err := manual.DetectSeriesAndHardwareCharacteristics(host)
91
121
        if err != nil {
92
122
                return err
93
123
        }
97
127
        }
98
128
        return manual.Bootstrap(manual.BootstrapArgs{
99
129
                Context:                 ctx,
100
 
                Host:                    e.envConfig().sshHost(),
 
130
                Host:                    host,
101
131
                DataDir:                 dataDir,
102
132
                Environ:                 e,
103
133
                PossibleTools:           selectedTools,
144
174
        return instances, err
145
175
}
146
176
 
 
177
var newSSHStorage = func(sshHost, storageDir, storageTmpdir string) (storage.Storage, error) {
 
178
        return sshstorage.NewSSHStorage(sshstorage.NewSSHStorageParams{
 
179
                Host:       sshHost,
 
180
                StorageDir: storageDir,
 
181
                TmpDir:     storageTmpdir,
 
182
        })
 
183
}
 
184
 
147
185
// Implements environs.BootstrapStorager.
148
186
func (e *nullEnviron) EnableBootstrapStorage(ctx environs.BootstrapContext) error {
149
187
        e.bootstrapStorageMutex.Lock()
151
189
        if e.bootstrapStorage != nil {
152
190
                return nil
153
191
        }
 
192
        if err := e.ensureBootstrapUbuntuUser(ctx); err != nil {
 
193
                return err
 
194
        }
154
195
        cfg := e.envConfig()
155
196
        storageDir := e.StorageDir()
156
197
        storageTmpdir := path.Join(dataDir, storageTmpSubdir)
157
 
        params := sshstorage.NewSSHStorageParams{
158
 
                Host:       cfg.sshHost(),
159
 
                StorageDir: storageDir,
160
 
                TmpDir:     storageTmpdir,
161
 
                Stdin:      ctx.Stdin(),
162
 
                Stdout:     ctx.Stdout(),
163
 
        }
164
 
        bootstrapStorage, err := sshstorage.NewSSHStorage(params)
 
198
        bootstrapStorage, err := newSSHStorage("ubuntu@"+cfg.bootstrapHost(), storageDir, storageTmpdir)
165
199
        if err != nil {
166
200
                return err
167
201
        }
199
233
        return nil
200
234
}
201
235
 
 
236
var runSSHCommand = func(host string, command []string) (stderr string, err error) {
 
237
        cmd := ssh.Command(host, command, nil)
 
238
        var stderrBuf bytes.Buffer
 
239
        cmd.Stderr = &stderrBuf
 
240
        err = cmd.Run()
 
241
        return stderrBuf.String(), err
 
242
}
 
243
 
202
244
func (e *nullEnviron) Destroy() error {
203
 
        return errors.New("null provider destruction is not implemented yet")
 
245
        stderr, err := runSSHCommand(
 
246
                "ubuntu@"+e.envConfig().bootstrapHost(),
 
247
                []string{"sudo", "pkill", fmt.Sprintf("-%d", terminationworker.TerminationSignal), "jujud"},
 
248
        )
 
249
        if err != nil {
 
250
                if stderr := strings.TrimSpace(stderr); len(stderr) > 0 {
 
251
                        err = fmt.Errorf("%v (%v)", err, stderr)
 
252
                }
 
253
        }
 
254
        return err
204
255
}
205
256
 
206
257
func (e *nullEnviron) OpenPorts(ports []instance.Port) error {