Esempio n. 1
0
func ConvertToApi(c *project.ServiceConfig) (*dockerclient.ContainerConfig, error) {
	config, hostConfig, err := Convert(c)
	if err != nil {
		return nil, err
	}

	var result dockerclient.ContainerConfig
	err = utils.ConvertByJSON(config, &result)
	if err != nil {
		logrus.Errorf("Failed to convert config to API structure: %v\n%#v", err, config)
		return nil, err
	}

	err = utils.ConvertByJSON(hostConfig, &result.HostConfig)
	if err != nil {
		logrus.Errorf("Failed to convert hostConfig to API structure: %v\n%#v", err, hostConfig)
	}
	return &result, err
}
func upgradeEnvironment(logger *logrus.Entry, event *events.Event, apiClient *client.RancherClient) error {
	var upgradeOpts client.StackUpgrade

	if err := utils.ConvertByJSON(event.Data, &upgradeOpts); err != nil {
		return err
	}

	stack, err := apiClient.Stack.ById(event.ResourceID)
	if err != nil {
		return err
	}

	if stack == nil {
		return errors.New("Failed to find stack")
	}

	if stack.DockerCompose == "" {
		return emptyReply(event, apiClient)
	}

	project, newEnv, err := constructProjectUpgrade(logger, stack, upgradeOpts, apiClient.GetOpts().Url, apiClient.GetOpts().AccessKey, apiClient.GetOpts().SecretKey)
	if err != nil {
		return err
	}

	publishTransitioningReply("Upgrading stack", event, apiClient, false)

	if err := project.Up(context.Background(), options.Up{}); err != nil {
		return err
	}

	previous := stack.PreviousExternalId
	if previous == "" {
		previous = stack.ExternalId
	}

	previousEnv := stack.PreviousEnvironment
	if len(previousEnv) == 0 {
		previousEnv = stack.Environment
	}

	return reply(event, apiClient, map[string]interface{}{
		"externalId":          upgradeOpts.ExternalId,
		"environment":         newEnv,
		"previousExternalId":  previous,
		"previousEnvironment": previousEnv,
	})
}