func (s *BootstrapSuite) testToolsMetadata(c *gc.C, exploded bool) { provider, err := environs.Provider(s.envcfg.Type()) c.Assert(err, gc.IsNil) env, err := provider.Open(s.envcfg) c.Assert(err, gc.IsNil) oldMetadata, err := envtools.ReadMetadata(env.Storage()) c.Assert(err, gc.IsNil) _, cmd, err := s.initBootstrapCommand(c, nil, "--env-config", s.b64yamlEnvcfg, "--instance-id", string(s.instanceId)) c.Assert(err, gc.IsNil) err = cmd.Run(nil) c.Assert(err, gc.IsNil) newMetadata, err := envtools.ReadMetadata(env.Storage()) c.Assert(err, gc.IsNil) if !exploded { c.Assert(newMetadata, gc.HasLen, len(oldMetadata)) } else { // new metadata should have more tools. c.Assert(len(newMetadata), jc.GreaterThan, len(oldMetadata)) var expectedSeries set.Strings for _, series := range version.SupportedSeries() { os, err := version.GetOSFromSeries(series) c.Assert(err, gc.IsNil) if os == version.Ubuntu { expectedSeries.Add(series) } } c.Assert(newMetadata, gc.HasLen, expectedSeries.Size()) for _, m := range newMetadata { c.Assert(expectedSeries.Contains(m.Release), jc.IsTrue) } } }
func (*CurrentSuite) TestCurrentSeries(c *gc.C) { s := version.Current.Series if s == "unknown" { s = "n/a" } out, err := exec.Command("lsb_release", "-c").CombinedOutput() if err != nil { // If the command fails (for instance if we're running on some other // platform) then CurrentSeries should be unknown. switch runtime.GOOS { case "darwin": c.Check(s, gc.Matches, `mavericks|mountainlion|lion|snowleopard`) case "windows": c.Check(s, gc.Matches, `win2012hvr2|win2012hv|win2012|win2012r2|win8|win81|win7`) default: c.Assert(s, gc.Equals, "n/a") } } else { os, err := version.GetOSFromSeries(s) c.Assert(err, gc.IsNil) // There is no lsb_release command on CentOS. switch os { case version.CentOS: c.Check(s, gc.Matches, `centos7`) default: c.Assert(string(out), gc.Equals, "Codename:\t"+s+"\n") } } }
func versionInitSystem(series string) (string, error) { os, err := version.GetOSFromSeries(series) if err != nil { notFound := errors.NotFoundf("init system for series %q", series) return "", errors.Wrap(err, notFound) } switch os { case version.Windows: return InitSystemWindows, nil case version.Ubuntu: switch series { case "precise", "quantal", "raring", "saucy", "trusty", "utopic": return InitSystemUpstart, nil default: // vivid and later if featureflag.Enabled(feature.LegacyUpstart) { return InitSystemUpstart, nil } return InitSystemSystemd, nil } case version.CentOS: return InitSystemSystemd, nil } return "", errors.NotFoundf("unknown os %q (from series %q), init system", os, series) }
// UserdataConfig is supposed to take in an instanceConfig as well as a // cloudinit.cloudConfig and add attributes in the cloudinit structure based on // the values inside instanceConfig and on the series func NewUserdataConfig(icfg *instancecfg.InstanceConfig, conf cloudinit.CloudConfig) (UserdataConfig, error) { // TODO(ericsnow) bug #1426217 // Protect icfg and conf better. operatingSystem, err := version.GetOSFromSeries(icfg.Series) if err != nil { return nil, err } base := baseConfigure{ tag: names.NewMachineTag(icfg.MachineId), icfg: icfg, conf: conf, os: operatingSystem, } switch operatingSystem { case version.Ubuntu: return &unixConfigure{base}, nil case version.CentOS: return &unixConfigure{base}, nil case version.Windows: return &windowsConfigure{base}, nil default: return nil, errors.NotSupportedf("OS %s", icfg.Series) } }
func (w *unixConfigure) setDataDirPermissions() string { os, _ := version.GetOSFromSeries(w.icfg.Series) var user string switch os { case version.CentOS: user = "******" default: user = "******" } return fmt.Sprintf("chown %s:adm %s", user, w.icfg.LogDir) }
func (s *supportedSeriesSuite) TestGetOSFromSeries(c *gc.C) { for _, t := range getOSFromSeriesTests { got, err := version.GetOSFromSeries(t.series) if t.err != "" { c.Assert(err, gc.ErrorMatches, t.err) } else { c.Check(err, jc.ErrorIsNil) c.Assert(got, gc.Equals, t.want) } } }
func (w *proxyWorker) writeEnvironment() error { osystem, err := version.GetOSFromSeries(version.Current.Series) if err != nil { return err } switch osystem { case version.Windows: return w.writeEnvironmentToRegistry() default: return w.writeEnvironmentFile() } }
// osVal will lookup the value of the key valname // in the apropriate map, based on the series. This will // help reduce boilerplate code func osVal(series string, valname osVarType) (string, error) { os, err := version.GetOSFromSeries(series) if err != nil { return "", err } switch os { case version.Windows: return winVals[valname], nil default: return nixVals[valname], nil } }
func (s *BootstrapSuite) testToolsMetadata(c *gc.C, exploded bool) { provider, err := environs.Provider(s.envcfg.Type()) c.Assert(err, gc.IsNil) env, err := provider.Open(s.envcfg) c.Assert(err, gc.IsNil) envtesting.RemoveFakeToolsMetadata(c, env.Storage()) _, cmd, err := s.initBootstrapCommand(c, nil, "--env-config", s.b64yamlEnvcfg, "--instance-id", string(s.instanceId)) c.Assert(err, gc.IsNil) err = cmd.Run(nil) c.Assert(err, gc.IsNil) // We don't write metadata at bootstrap anymore. simplestreamsMetadata, err := envtools.ReadMetadata(env.Storage()) c.Assert(err, gc.IsNil) c.Assert(simplestreamsMetadata, gc.HasLen, 0) // The tools should have been added to state, and // exploded into each of the supported series of // the same operating system if the tools were uploaded. st, err := state.Open(&mongo.MongoInfo{ Info: mongo.Info{ Addrs: []string{gitjujutesting.MgoServer.Addr()}, CACert: testing.CACert, }, Password: testPasswordHash(), }, mongo.DefaultDialOpts(), environs.NewStatePolicy()) c.Assert(err, gc.IsNil) defer st.Close() var expectedSeries set.Strings if exploded { for _, series := range version.SupportedSeries() { os, err := version.GetOSFromSeries(series) c.Assert(err, gc.IsNil) if os == version.Current.OS { expectedSeries.Add(series) } } } else { expectedSeries.Add(version.Current.Series) } storage, err := st.ToolsStorage() c.Assert(err, gc.IsNil) defer storage.Close() metadata, err := storage.AllMetadata() c.Assert(err, gc.IsNil) c.Assert(metadata, gc.HasLen, expectedSeries.Size()) for _, m := range metadata { c.Assert(expectedSeries.Contains(m.Version.Series), jc.IsTrue) } }
// locallyBuildableTools returns the list of tools that // can be built locally, for series of the same OS. func locallyBuildableTools() (buildable coretools.List) { for _, series := range version.SupportedSeries() { if os, err := version.GetOSFromSeries(series); err != nil || os != version.Current.OS { continue } binary := version.Current binary.Series = series // Increment the build number so we know it's a development build. binary.Build++ buildable = append(buildable, &coretools.Tools{Version: binary}) } return buildable }
// osVal will lookup the value of the key valname // in the apropriate map, based on the series. This will // help reduce boilerplate code func osVal(series string, valname osVarType) (string, error) { os, err := version.GetOSFromSeries(series) if err != nil { return "", err } switch os { case version.Windows: return winVals[valname], nil case version.Ubuntu: return linuxVals[valname], nil } return "", fmt.Errorf("Unknown OS: %q", os) }
func NewUserdataConfig(cfg *MachineConfig, c *cloudinit.Config) (UserdataConfig, error) { operatingSystem, err := version.GetOSFromSeries(cfg.Series) if err != nil { return nil, err } switch operatingSystem { case version.Ubuntu: return newUbuntuConfig(cfg, c) case version.Windows: return newWindowsConfig(cfg, c) default: return nil, errors.Errorf("Unsupported OS %s", cfg.Series) } }
// NewRenderer returns a Renderer interface for selected series func NewRenderer(series string) (Renderer, error) { operatingSystem, err := version.GetOSFromSeries(series) if err != nil { return nil, err } switch operatingSystem { case version.Windows: return &WindowsRenderer{}, nil case version.Ubuntu: return &UbuntuRenderer{}, nil default: return nil, errors.Errorf("No renderer could be found for %s", series) } }
// binary returns the tools metadata's binary version, which may be used for // map lookup. It is possible for a binary to have an unkown OS. func (t *ToolsMetadata) binary() (version.Binary, error) { num, err := version.Parse(t.Version) if err != nil { return version.Binary{}, errors.Trace(err) } toolsOS, err := version.GetOSFromSeries(t.Release) if err != nil && !version.IsUnknownOSForSeriesError(err) { return version.Binary{}, errors.Trace(err) } return version.Binary{ Number: num, Series: t.Release, Arch: t.Arch, OS: toolsOS, }, nil }
// New returns a new Config with no options set. func New(series string) (CloudConfig, error) { os, err := version.GetOSFromSeries(series) if err != nil { return nil, err } switch os { case version.Windows: renderer, _ := shell.NewRenderer("powershell") return &windowsCloudConfig{ &cloudConfig{ series: series, renderer: renderer, attrs: make(map[string]interface{}), }, }, nil case version.Ubuntu: renderer, _ := shell.NewRenderer("bash") return &ubuntuCloudConfig{ &cloudConfig{ series: series, paccmder: commands.NewAptPackageCommander(), pacconfer: config.NewAptPackagingConfigurer(series), renderer: renderer, attrs: make(map[string]interface{}), }, }, nil case version.CentOS: renderer, _ := shell.NewRenderer("bash") return ¢OSCloudConfig{ &cloudConfig{ series: series, paccmder: commands.NewYumPackageCommander(), pacconfer: config.NewYumPackagingConfigurer(series), renderer: renderer, attrs: make(map[string]interface{}), }, }, nil default: return nil, errors.NotFoundf("cloudconfig for series %q", series) } }
func (c *baseConfigure) addMachineAgentToBoot() error { svc, err := c.icfg.InitService(c.conf.ShellRenderer()) if err != nil { return errors.Trace(err) } // Make the agent run via a symbolic link to the actual tools // directory, so it can upgrade itself without needing to change // the init script. toolsDir := c.icfg.ToolsDir(c.conf.ShellRenderer()) c.conf.AddScripts(c.toolsSymlinkCommand(toolsDir)) name := c.tag.String() cmds, err := svc.InstallCommands() if err != nil { return errors.Annotatef(err, "cannot make cloud-init init script for the %s agent", name) } startCmds, err := svc.StartCommands() if err != nil { return errors.Annotatef(err, "cannot make cloud-init init script for the %s agent", name) } cmds = append(cmds, startCmds...) svcName := c.icfg.MachineAgentServiceName // TODO (gsamfira): This is temporary until we find a cleaner way to fix // cloudinit.LogProgressCmd to not add >&9 on Windows. targetOS, err := version.GetOSFromSeries(c.icfg.Series) if err != nil { return err } if targetOS != version.Windows { c.conf.AddRunCmd(cloudinit.LogProgressCmd("Starting Juju machine agent (%s)", svcName)) } c.conf.AddScripts(cmds...) return nil }
func (s *supportedSeriesSuite) TestUnknownOSFromSeries(c *gc.C) { _, err := version.GetOSFromSeries("Xuanhuaceratops") c.Assert(err, jc.Satisfies, version.IsUnknownOSForSeriesError) c.Assert(err, gc.ErrorMatches, `unknown OS for series: "Xuanhuaceratops"`) }