// consumeCommands executes the given command and it's arguments.
func (imp *Implementation) consumeCommands(commands [][]string) {
	for _, cmd := range commands {
		flog.Debug("Executing command",
			flog.Fields{
				Event: "distro.Implementation.consumeCommands",
			},
			flog.Details{
				"command": cmd,
			},
		)

		out, err := imp.Execute(cmd[0], cmd[1:]...)
		if err != nil {
			flog.Error("Failed to execute command",
				flog.Fields{
					Event: "distro.Implementation.Execute",
					Error: err,
				},
				flog.Details{
					"command": cmd,
				},
			)
		}

		flog.Debug("Executed command",
			flog.Fields{
				Event: "distro.Implementation.consumeCommands",
			},
			flog.Details{
				"command": cmd,
				"output":  out,
			},
		)
	}
}
Exemple #2
0
func (c *ClientImplementation) performRequest(r *Request) (*Response, error) {
	req := r.Normalize()

	resp, err := c.HTTPClient.Do(req)
	if err != nil {
		return nil, err
	}

	if !strings.HasPrefix(resp.Status, "20") {
		flog.Debug("Failed to get a proper HTTP response",
			flog.Fields{
				Error: err,
				Event: "performRequest",
			},
			flog.Details{
				"url":     r.URL,
				"method":  r.Method,
				"headers": r.Headers,
			},
		)
		return nil, fmt.Errorf("request: bad status code %v", resp.StatusCode)
	}

	return &Response{resp}, nil
}
Exemple #3
0
// isAvailable tries to fetch meta-data from the given data source provider
// and returns false if it encounters any errors during the process.
func isAvailable(p Provider) bool {
	_, err := p.FetchMetadata()
	if err != nil {
		flog.Debug("Provider isn't available",
			flog.Fields{
				Event: "datasrc.isAvailable",
			},
			flog.Details{
				"provider": p,
			},
		)
	}

	return err == nil
}
// consumeCloudConfig parses the given cloud config file contents and
// consumes the parsed directives.
func (imp *Implementation) consumeCloudConfig(contents string) error {
	conf, err := cloudconfig.Parse(strutil.ToReadCloser(contents))
	if err != nil {
		flog.Error("Failed to parse cloud config file",
			flog.Fields{
				Event: "cloudconfig.Parse",
				Error: err,
			},
		)

		return err
	}

	flog.Debug("Persisting files",
		flog.Fields{
			Event: "distro.Implementation.consumeCloudConfig",
		},
	)

	imp.writeFiles(conf.Files)

	imp.consumeCommands(conf.Commands)

	for grpName, _ := range conf.Groups {
		flog.Info("Creating user group",
			flog.Fields{
				Event: "distro.consumeCloudConfig",
			},
			flog.Details{
				"group": grpName,
			},
		)

		newGrp := identity.Group{
			Name: grpName,
		}

		if err := imp.ID.CreateGroup(newGrp); err != nil {
			flog.Error("Failed to create a user group",
				flog.Fields{
					Event: "identityManager.CreateGroup",
					Error: err,
				},
				flog.Details{
					"group": grpName,
				},
			)
		}
	}

	for _, usr := range conf.Users {
		flog.Info("Creating user",
			flog.Fields{
				Event: "distro.consumeCloudConfig",
			},
			flog.Details{
				"user": usr.Name,
			},
		)

		if err := imp.ID.CreateUser(usr); err != nil {
			flog.Error("Failed to create a user",
				flog.Fields{
					Event: "identityManager.CreateUser",
					Error: err,
				},
				flog.Details{
					"user": usr.Name,
				},
			)
		}
	}

	for grpName, usrNames := range conf.Groups {
		for _, usrName := range usrNames {
			flog.Info("Adding user to group",
				flog.Fields{
					Event: "distro.consumeCloudConfig",
				},
				flog.Details{
					"user":  usrName,
					"group": grpName,
				},
			)

			if err := imp.ID.AddUserToGroup(usrName, grpName); err != nil {
				flog.Error("Failed to add user to group",
					flog.Fields{
						Event: "identityManager.AddUserToGroup",
						Error: err,
					},
					flog.Details{
						"user":  usrName,
						"group": grpName,
					},
				)
			}
		}
	}

	imp.consumeSSHKeys(conf.AuthorizedKeys)

	return err
}