本文整理汇总了Golang中github.com/wallyworld/core/cmd.Context类的典型用法代码示例。如果您正苦于以下问题:Golang Context类的具体用法?Golang Context怎么用?Golang Context使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Context类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: Run
func (c *ImageMetadataCommand) Run(context *cmd.Context) error {
if err := c.setParams(context); err != nil {
return err
}
out := context.Stdout
im := &imagemetadata.ImageMetadata{
Id: c.ImageId,
Arch: c.Arch,
}
cloudSpec := simplestreams.CloudSpec{
Region: c.Region,
Endpoint: c.Endpoint,
}
targetStorage, err := filestorage.NewFileStorageWriter(c.Dir)
if err != nil {
return err
}
err = imagemetadata.MergeAndWriteMetadata(c.Series, []*imagemetadata.ImageMetadata{im}, &cloudSpec, targetStorage)
if err != nil {
return fmt.Errorf("image metadata files could not be created: %v", err)
}
dir := context.AbsPath(c.Dir)
dest := filepath.Join(dir, storage.BaseImagesPath, "streams", "v1")
fmt.Fprintf(out, fmt.Sprintf(helpDoc, dest, dir, dir))
return nil
}
开发者ID:jameinel,项目名称:core,代码行数:26,代码来源:imagemetadata.go
示例2: Run
func (c *DestroyEnvironmentCommand) Run(ctx *cmd.Context) (result error) {
store, err := configstore.Default()
if err != nil {
return fmt.Errorf("cannot open environment info storage: %v", err)
}
environ, err := environs.NewFromName(c.envName, store)
if err != nil {
if environs.IsEmptyConfig(err) {
// Delete the .jenv file and call it done.
ctx.Infof("removing empty environment file")
return environs.DestroyInfo(c.envName, store)
}
return err
}
if !c.assumeYes {
fmt.Fprintf(ctx.Stdout, destroyEnvMsg, environ.Name(), environ.Config().Type())
scanner := bufio.NewScanner(ctx.Stdin)
scanner.Scan()
err := scanner.Err()
if err != nil && err != io.EOF {
return fmt.Errorf("Environment destruction aborted: %s", err)
}
answer := strings.ToLower(scanner.Text())
if answer != "y" && answer != "yes" {
return errors.New("environment destruction aborted")
}
}
// If --force is supplied, then don't attempt to use the API.
// This is necessary to destroy broken environments, where the
// API server is inaccessible or faulty.
if !c.force {
defer func() {
if result == nil {
return
}
logger.Errorf(`failed to destroy environment %q
If the environment is unusable, then you may run
juju destroy-environment --force
to forcefully destroy the environment. Upon doing so, review
your environment provider console for any resources that need
to be cleaned up.
`, c.envName)
}()
apiclient, err := juju.NewAPIClientFromName(c.envName)
if err != nil {
return fmt.Errorf("cannot connect to API: %v", err)
}
defer apiclient.Close()
err = apiclient.DestroyEnvironment()
if err != nil && !params.IsCodeNotImplemented(err) {
return fmt.Errorf("destroying environment: %v", err)
}
}
return environs.Destroy(environ, store)
}
开发者ID:jameinel,项目名称:core,代码行数:60,代码来源:destroyenvironment.go
示例3: addCharmViaAPI
// addCharmViaAPI calls the appropriate client API calls to add the
// given charm URL to state. Also displays the charm URL of the added
// charm on stdout.
func addCharmViaAPI(client *api.Client, ctx *cmd.Context, curl *charm.URL, repo charm.Repository) (*charm.URL, error) {
if curl.Revision < 0 {
latest, err := charm.Latest(repo, curl)
if err != nil {
return nil, err
}
curl = curl.WithRevision(latest)
}
switch curl.Schema {
case "local":
ch, err := repo.Get(curl)
if err != nil {
return nil, err
}
stateCurl, err := client.AddLocalCharm(curl, ch)
if err != nil {
return nil, err
}
curl = stateCurl
case "cs":
err := client.AddCharm(curl)
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("unsupported charm URL schema: %q", curl.Schema)
}
ctx.Infof("Added charm %q to the environment.", curl)
return curl, nil
}
开发者ID:jameinel,项目名称:core,代码行数:33,代码来源:deploy.go
示例4: destroyPreparedEnviron
// destroyPreparedEnviron destroys the environment and logs an error if it fails.
func destroyPreparedEnviron(ctx *cmd.Context, env environs.Environ, store configstore.Storage, err *error, action string) {
if *err == nil {
return
}
ctx.Infof("%s failed, destroying environment", action)
if err := environs.Destroy(env, store); err != nil {
logger.Errorf("%s failed, and the environment could not be destroyed: %v", action, err)
}
}
开发者ID:jameinel,项目名称:core,代码行数:10,代码来源:common.go
示例5: Run
func (c *SignMetadataCommand) Run(context *cmd.Context) error {
loggo.RegisterWriter("signmetadata", cmd.NewCommandLogWriter("juju.plugins.metadata", context.Stdout, context.Stderr), loggo.INFO)
defer loggo.RemoveWriter("signmetadata")
keyData, err := ioutil.ReadFile(c.keyFile)
if err != nil {
return err
}
dir := context.AbsPath(c.dir)
return process(dir, string(keyData), c.passphrase)
}
开发者ID:jameinel,项目名称:core,代码行数:10,代码来源:signmetadata.go
示例6: Run
func (c *RpcCommand) Run(ctx *cmd.Context) error {
if c.Value == "error" {
return errors.New("blam")
}
if c.Slow {
time.Sleep(testing.ShortWait)
return nil
}
ctx.Stdout.Write([]byte("eye of newt\n"))
ctx.Stderr.Write([]byte("toe of frog\n"))
return ioutil.WriteFile(ctx.AbsPath("local"), []byte(c.Value), 0644)
}
开发者ID:jameinel,项目名称:core,代码行数:12,代码来源:server_test.go
示例7: watchDebugLog1dot18
// watchDebugLog1dot18 runs in case of an older API server and uses ssh
// but with server-side grep.
func (c *DebugLogCommand) watchDebugLog1dot18(ctx *cmd.Context) error {
ctx.Infof("Server does not support new stream log, falling back to tail")
ctx.Verbosef("filters are not supported with tail")
sshCmd := &SSHCommand{}
tailCmd := fmt.Sprintf("tail -n -%d -f %s", c.params.Backlog, DefaultLogLocation)
// If the api doesn't support WatchDebugLog, then it won't be running in
// HA either, so machine 0 is where it is all at.
args := []string{"0", tailCmd}
err := sshCmd.Init(args)
if err != nil {
return err
}
sshCmd.EnvName = c.EnvName
return runSSHCommand(sshCmd, ctx)
}
开发者ID:jameinel,项目名称:core,代码行数:17,代码来源:debuglog.go
示例8: Run
func (c *ToolsMetadataCommand) Run(context *cmd.Context) error {
loggo.RegisterWriter("toolsmetadata", cmd.NewCommandLogWriter("juju.environs.tools", context.Stdout, context.Stderr), loggo.INFO)
defer loggo.RemoveWriter("toolsmetadata")
if c.metadataDir == "" {
c.metadataDir = osenv.JujuHome()
} else {
c.metadataDir = context.AbsPath(c.metadataDir)
}
sourceStorage, err := filestorage.NewFileStorageReader(c.metadataDir)
if err != nil {
return err
}
fmt.Fprintf(context.Stdout, "Finding tools in %s\n", c.metadataDir)
const minorVersion = -1
toolsList, err := envtools.ReadList(sourceStorage, version.Current.Major, minorVersion)
if err == envtools.ErrNoTools {
var source string
source, err = envtools.ToolsURL(envtools.DefaultBaseURL)
if err != nil {
return err
}
sourceDataSource := simplestreams.NewURLDataSource("local source", source, utils.VerifySSLHostnames)
toolsList, err = envtools.FindToolsForCloud(
[]simplestreams.DataSource{sourceDataSource}, simplestreams.CloudSpec{},
version.Current.Major, minorVersion, coretools.Filter{})
}
if err != nil {
return err
}
targetStorage, err := filestorage.NewFileStorageWriter(c.metadataDir)
if err != nil {
return err
}
writeMirrors := envtools.DoNotWriteMirrors
if c.public {
writeMirrors = envtools.WriteMirrors
}
return mergeAndWriteMetadata(targetStorage, toolsList, writeMirrors)
}
开发者ID:jameinel,项目名称:core,代码行数:41,代码来源:toolsmetadata.go
示例9: Run
func (c *DeployCommand) Run(ctx *cmd.Context) error {
client, err := juju.NewAPIClientFromName(c.EnvName)
if err != nil {
return err
}
defer client.Close()
attrs, err := client.EnvironmentGet()
if err != nil {
return err
}
conf, err := config.New(config.NoDefaults, attrs)
if err != nil {
return err
}
curl, err := resolveCharmURL(c.CharmName, client, conf)
if err != nil {
return err
}
repo, err := charm.InferRepository(curl.Reference, ctx.AbsPath(c.RepoPath))
if err != nil {
return err
}
repo = config.SpecializeCharmRepo(repo, conf)
curl, err = addCharmViaAPI(client, ctx, curl, repo)
if err != nil {
return err
}
if c.BumpRevision {
ctx.Infof("--upgrade (or -u) is deprecated and ignored; charms are always deployed with a unique revision.")
}
var includeNetworks []string
var excludeNetworks []string
haveNetworks := false
if c.Networks != "" {
includeNetworks = parseNetworks(c.Networks)
haveNetworks = true
}
if c.ExcludeNetworks != "" {
excludeNetworks = parseNetworks(c.ExcludeNetworks)
haveNetworks = true
}
if haveNetworks {
env, err := environs.New(conf)
if err != nil {
return err
}
if !env.SupportNetworks() {
return errors.New("cannot use --networks/--exclude-networks: not supported by the environment")
}
}
charmInfo, err := client.CharmInfo(curl.String())
if err != nil {
return err
}
numUnits := c.NumUnits
if charmInfo.Meta.Subordinate {
if !constraints.IsEmpty(&c.Constraints) {
return errors.New("cannot use --constraints with subordinate service")
}
if numUnits == 1 && c.ToMachineSpec == "" {
numUnits = 0
} else {
return errors.New("cannot use --num-units or --to with subordinate service")
}
}
serviceName := c.ServiceName
if serviceName == "" {
serviceName = charmInfo.Meta.Name
}
var configYAML []byte
if c.Config.Path != "" {
configYAML, err = c.Config.Read(ctx)
if err != nil {
return err
}
}
err = client.ServiceDeployWithNetworks(
curl.String(),
serviceName,
numUnits,
string(configYAML),
c.Constraints,
c.ToMachineSpec,
includeNetworks,
excludeNetworks,
)
if params.IsCodeNotImplemented(err) {
if haveNetworks {
return errors.New("cannot use --networks/--exclude-networks: not supported by the API server")
}
err = client.ServiceDeploy(
//.........这里部分代码省略.........
开发者ID:jameinel,项目名称:core,代码行数:101,代码来源:deploy.go
示例10: Run
// Run connects to the environment specified on the command line and bootstraps
// a juju in that environment if none already exists. If there is as yet no environments.yaml file,
// the user is informed how to create one.
func (c *BootstrapCommand) Run(ctx *cmd.Context) (resultErr error) {
bootstrapFuncs := getBootstrapFuncs()
if len(c.seriesOld) > 0 {
fmt.Fprintln(ctx.Stderr, "Use of --series is deprecated. Please use --upload-series instead.")
}
environ, cleanup, err := environFromName(ctx, c.EnvName, &resultErr, "Bootstrap")
if err != nil {
return err
}
validator, err := environ.ConstraintsValidator()
if err != nil {
return err
}
unsupported, err := validator.Validate(c.Constraints)
if len(unsupported) > 0 {
logger.Warningf("unsupported constraints: %v", err)
} else if err != nil {
return err
}
defer cleanup()
if err := bootstrapFuncs.EnsureNotBootstrapped(environ); err != nil {
return err
}
// Block interruption during bootstrap. Providers may also
// register for interrupt notification so they can exit early.
interrupted := make(chan os.Signal, 1)
defer close(interrupted)
ctx.InterruptNotify(interrupted)
defer ctx.StopInterruptNotify(interrupted)
go func() {
for _ = range interrupted {
ctx.Infof("Interrupt signalled: waiting for bootstrap to exit")
}
}()
// If --metadata-source is specified, override the default tools metadata source so
// SyncTools can use it, and also upload any image metadata.
if c.MetadataSource != "" {
metadataDir := ctx.AbsPath(c.MetadataSource)
logger.Infof("Setting default tools and image metadata sources: %s", metadataDir)
tools.DefaultBaseURL = metadataDir
if err := imagemetadata.UploadImageMetadata(environ.Storage(), metadataDir); err != nil {
// Do not error if image metadata directory doesn't exist.
if !os.IsNotExist(err) {
return fmt.Errorf("uploading image metadata: %v", err)
}
} else {
logger.Infof("custom image metadata uploaded")
}
}
// TODO (wallyworld): 2013-09-20 bug 1227931
// We can set a custom tools data source instead of doing an
// unnecessary upload.
if environ.Config().Type() == provider.Local {
c.UploadTools = true
}
if c.UploadTools {
err = bootstrapFuncs.UploadTools(ctx, environ, c.Constraints.Arch, true, c.Series...)
if err != nil {
return err
}
}
return bootstrapFuncs.Bootstrap(ctx, environ, environs.BootstrapParams{
Constraints: c.Constraints,
Placement: c.Placement,
})
}
开发者ID:jameinel,项目名称:core,代码行数:74,代码来源:bootstrap.go
示例11: Run
// Run connects to the specified environment and starts the charm
// upgrade process.
func (c *UpgradeCharmCommand) Run(ctx *cmd.Context) error {
client, err := juju.NewAPIClientFromName(c.EnvName)
if err != nil {
return err
}
defer client.Close()
oldURL, err := client.ServiceGetCharmURL(c.ServiceName)
if err != nil {
return err
}
attrs, err := client.EnvironmentGet()
if err != nil {
return err
}
conf, err := config.New(config.NoDefaults, attrs)
if err != nil {
return err
}
var newURL *charm.URL
if c.SwitchURL != "" {
newURL, err = resolveCharmURL(c.SwitchURL, client, conf)
if err != nil {
return err
}
} else {
// No new URL specified, but revision might have been.
newURL = oldURL.WithRevision(c.Revision)
}
repo, err := charm.InferRepository(newURL.Reference, ctx.AbsPath(c.RepoPath))
if err != nil {
return err
}
repo = config.SpecializeCharmRepo(repo, conf)
// If no explicit revision was set with either SwitchURL
// or Revision flags, discover the latest.
explicitRevision := true
if newURL.Revision == -1 {
explicitRevision = false
latest, err := charm.Latest(repo, newURL)
if err != nil {
return err
}
newURL = newURL.WithRevision(latest)
}
if *newURL == *oldURL {
if explicitRevision {
return fmt.Errorf("already running specified charm %q", newURL)
} else if newURL.Schema == "cs" {
// No point in trying to upgrade a charm store charm when
// we just determined that's the latest revision
// available.
return fmt.Errorf("already running latest charm %q", newURL)
}
}
addedURL, err := addCharmViaAPI(client, ctx, newURL, repo)
if err != nil {
return err
}
return client.ServiceSetCharm(c.ServiceName, addedURL.String(), c.Force)
}
开发者ID:jameinel,项目名称:core,代码行数:68,代码来源:upgradecharm.go
示例12: ReadConfig
func (c *ConfigCommand) ReadConfig(ctx *cmd.Context) (err error) {
c.Config, err = store.ReadConfig(ctx.AbsPath(c.ConfigPath))
return err
}
开发者ID:jameinel,项目名称:core,代码行数:4,代码来源:config.go
示例13: Run
func (c *AddMachineCommand) Run(ctx *cmd.Context) error {
if c.Placement != nil && c.Placement.Scope == "ssh" {
args := manual.ProvisionMachineArgs{
Host: c.Placement.Directive,
EnvName: c.EnvName,
Stdin: ctx.Stdin,
Stdout: ctx.Stdout,
Stderr: ctx.Stderr,
}
_, err := manual.ProvisionMachine(args)
return err
}
client, err := juju.NewAPIClientFromName(c.EnvName)
if err != nil {
return err
}
defer client.Close()
if c.Placement != nil && c.Placement.Scope == instance.MachineScope {
// It does not make sense to add-machine <id>.
return fmt.Errorf("machine-id cannot be specified when adding machines")
}
machineParams := params.AddMachineParams{
Placement: c.Placement,
Series: c.Series,
Constraints: c.Constraints,
Jobs: []params.MachineJob{params.JobHostUnits},
}
results, err := client.AddMachines([]params.AddMachineParams{machineParams})
if params.IsCodeNotImplemented(err) {
if c.Placement != nil {
containerType, parseErr := instance.ParseContainerType(c.Placement.Scope)
if parseErr != nil {
// The user specified a non-container placement directive:
// return original API not implemented error.
return err
}
machineParams.ContainerType = containerType
machineParams.ParentId = c.Placement.Directive
machineParams.Placement = nil
}
logger.Infof(
"AddMachinesWithPlacement not supported by the API server, " +
"falling back to 1.18 compatibility mode",
)
results, err = client.AddMachines1dot18([]params.AddMachineParams{machineParams})
}
if err != nil {
return err
}
// Currently, only one machine is added, but in future there may be several added in one call.
machineInfo := results[0]
if machineInfo.Error != nil {
return machineInfo.Error
}
machineId := machineInfo.Machine
if names.IsContainerMachine(machineId) {
ctx.Infof("created container %v", machineId)
} else {
ctx.Infof("created machine %v", machineId)
}
return nil
}
开发者ID:jameinel,项目名称:core,代码行数:67,代码来源:addmachine.go
示例14: Run
func (c *PublishCommand) Run(ctx *cmd.Context) (err error) {
branch := bzr.New(ctx.AbsPath(c.CharmPath))
if _, err := os.Stat(branch.Join(".bzr")); err != nil {
return fmt.Errorf("not a charm branch: %s", branch.Location())
}
if err := branch.CheckClean(); err != nil {
return err
}
var curl *charm.URL
if c.URL == "" {
if err == nil {
loc, err := branch.PushLocation()
if err != nil {
return fmt.Errorf("no charm URL provided and cannot infer from current directory (no push location)")
}
curl, err = charm.Store.CharmURL(loc)
if err != nil {
return fmt.Errorf("cannot infer charm URL from branch location: %q", loc)
}
}
} else {
curl, err = charm.InferURL(c.URL, "")
if err != nil {
return err
}
}
pushLocation := charm.Store.BranchLocation(curl)
if c.changePushLocation != nil {
pushLocation = c.changePushLocation(pushLocation)
}
repo, err := charm.InferRepository(curl.Reference, "/not/important")
if err != nil {
return err
}
if repo != charm.Store {
return fmt.Errorf("charm URL must reference the juju charm store")
}
localDigest, err := branch.RevisionId()
if err != nil {
return fmt.Errorf("cannot obtain local digest: %v", err)
}
logger.Infof("local digest is %s", localDigest)
ch, err := charm.ReadDir(branch.Location())
if err != nil {
return err
}
if ch.Meta().Name != curl.Name {
return fmt.Errorf("charm name in metadata must match name in URL: %q != %q", ch.Meta().Name, curl.Name)
}
oldEvent, err := charm.Store.Event(curl, localDigest)
if _, ok := err.(*charm.NotFoundError); ok {
oldEvent, err = charm.Store.Event(curl, "")
if _, ok := err.(*charm.NotFoundError); ok {
logger.Infof("charm %s is not yet in the store", curl)
err = nil
}
}
if err != nil {
return fmt.Errorf("cannot obtain event details from the store: %s", err)
}
if oldEvent != nil && oldEvent.Digest == localDigest {
return handleEvent(ctx, curl, oldEvent)
}
logger.Infof("sending charm to the charm store...")
err = branch.Push(&bzr.PushAttr{Location: pushLocation, Remember: true})
if err != nil {
return err
}
logger.Infof("charm sent; waiting for it to be published...")
for {
time.Sleep(c.pollDelay)
newEvent, err := charm.Store.Event(curl, "")
if _, ok := err.(*charm.NotFoundError); ok {
continue
}
if err != nil {
return fmt.Errorf("cannot obtain event details from the store: %s", err)
}
if oldEvent != nil && oldEvent.Digest == newEvent.Digest {
continue
}
if newEvent.Digest != localDigest {
// TODO Check if the published digest is in the local history.
return fmt.Errorf("charm changed but not to local charm digest; publishing race?")
}
return handleEvent(ctx, curl, newEvent)
}
}
开发者ID:jameinel,项目名称:core,代码行数:97,代码来源:publish.go
注:本文中的github.com/wallyworld/core/cmd.Context类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论