Beispiel #1
// UninstallCommand configures the Uninstall struct and calls it based on the
// given codegangsta/cli context.
// TODO: remove all artifacts, ie bolt db, ssh keys, kd etc.
func UninstallCommand(c *cli.Context, log logging.Logger, _ string) (string, int) {
	warnings := []string{}

	// Ensure /etc/kite/kite.key is migrated to konfig.bolt before
	// old klient gets uninstalled. The endpoint/config package
	// performs lazy migrations, so it's enough to call any of
	// its methods and disregard the result.
	_ = configcli.List()

	s, err := newService(nil)
	if err != nil {
		log.Warning("Failed creating Service for uninstall. err:%s", err)
		warnings = append(warnings, FailedUninstallingKlientWarn)

	uninstaller := &Uninstall{
		ServiceUninstaller: s,
		KlientName:         config.KlientName,
		KlientctlName:      config.Name,
		KlientctlPath:      filepath.Join(KlientctlDirectory, KlientctlBinName),
		// TODO: Store the klient directory structure(s) somewhere
		KlientParentDirectory: "/opt",
		KlientDirectory:       "kite/klient",
		KlientFilename:        "klient",
		KlientshFilename:      "",
		remover:               os.Remove,
		warnings:              warnings,
		log:                   log,

	return uninstaller.Uninstall()
Beispiel #2
// Register registers with the username to the given kontrolURL via the users
// password or the given token.
func Register(koding *url.URL, username, token string, debug bool) error {
	var err error

	// Open up a prompt if the username is not passed via a flag and it's not a
	// token based authentication. If token is empty, it means the user can be
	// authenticated via password
	if token == "" && username == "" {
		username, err = ask("Username:")
		if err != nil {
			return err
		// User can just press enter to use the default on the prompt
		if username == "" {
			return errors.New("Username can not be empty.")

	k := kite.New("klient", konfig.Version)
	k.Config.Environment = konfig.Environment
	k.Config.Region = konfig.Region
	k.Config.Username = username

	if debug {

	// Production Koding servers are only working over HTTP
	k.Config.Transport = config.XHRPolling

	// Give a warning if an existing kite.key exists
	newKonfig := cfg.NewKonfigURL(koding)

	if konfig := configcli.List()[newKonfig.ID()]; konfig != nil && konfig.KiteKey != "" {
		result, err := ask(fmt.Sprintf("An existing kite.key for %s detected. Type 'yes' to override and continue:", konfig.KodingPublic()))
		if err != nil {
			return err

		if result != "yes" {
			return errors.New("aborting registration")

	kontrol := k.NewClient(newKonfig.Endpoints.Kontrol().Public.String())
	if err := kontrol.DialTimeout(30 * time.Second); err != nil {
		return err
	defer kontrol.Close()

	// Register is always called with sudo, so Init should have enough
	// permissions.
	if err := tlsproxy.Init(); err != nil {
		return err

	authType := "password"
	if token != "" {
		authType = "token"

	var args = struct {
		Username string
		Token    string
		AuthType string
		Username: username,
		Token:    token,
		AuthType: authType,

	// If authtType is password, this causes Kontrol to execute the
	// 'kite.getPass' method (builtin method in the Kite library) on our own
	// local kite (the one we declared above) method bidirectional. So once we
	// execute this, we immediately get a prompt asking for our password, which
	// is then transfered back to Kontrol. If we have a token, it will not ask
	// for a password and will create retunr the key immediately if the token
	// is valid for the given username (which is passed via the args).
	result, err := kontrol.TellWithTimeout("registerMachine", 5*time.Minute, args)
	if err != nil {
		return err

	newKonfig.KiteKey = result.MustString()

	if err := configcli.Use(newKonfig); err != nil {
		return err

	// Using authenticated here instead of registered, so it is a
	// middleground in UX for both raw `klient -register` usage, and also
	// `kd install` usage. `kd install` is very user facing, and
	// registration is potentially confusing to the end user (since
	// they are already registered to etc)
	fmt.Println("Authenticated successfully")

	return nil