Skip to content
This repository was archived by the owner on Aug 15, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
5d7d204
kloud/metadata: make provision.sh a static asset
rjeczalik Mar 30, 2017
a267f3c
kloud/metadata: add New for creating cloud-init from user-data
rjeczalik Apr 3, 2017
2e7cf65
stack/provider: use metadata for provisioning klient
rjeczalik Apr 3, 2017
6405100
kloud/metadata: add inline doc
rjeczalik Apr 3, 2017
559983e
klient: ensure CurrentUser is read after it's written by flag.Parse
rjeczalik Apr 3, 2017
819481e
klient: bump go version for builds
rjeczalik Apr 3, 2017
a279ca2
client: remove comments about Ubuntu 14.04 restrictions
rjeczalik Apr 3, 2017
d14de15
kloud/metadata: replace wget with curl
rjeczalik Apr 3, 2017
d9c6ee5
kloud/metadata: ensure /var/log/klient.log is owned by user
rjeczalik Apr 3, 2017
8371a5e
klient/terminal: rework screen command exec
rjeczalik Apr 3, 2017
8b573bf
klient/terminal: fix user / env handling
rjeczalik Apr 4, 2017
5f32b63
kloud/metadata: ensure proper perms for screen dir
rjeczalik Apr 4, 2017
69448ae
kloud: use native screen when available
rjeczalik Apr 4, 2017
72f26d5
stack/provider: simplify metadata handling
rjeczalik Apr 4, 2017
a92bf6c
kloud/metadata: ensure cloud-init-output.log has proper perms
rjeczalik Apr 4, 2017
a82b0ea
kloud/metadata: fix provision script termination
rjeczalik Apr 4, 2017
6844c88
kloud/metadata: workaround cloud-init logging for Red Hat
rjeczalik Apr 4, 2017
fd0d661
kloud/metadata: workaround for group on Suse
rjeczalik Apr 4, 2017
e2d6d7c
stack/provider: add koding_klient_timeout support
rjeczalik Apr 4, 2017
964aba8
kloud/provider: update tests
rjeczalik Apr 4, 2017
5a3d918
provider/marathon: update tests
rjeczalik Apr 5, 2017
f39b305
provider/softlayer: update tests
rjeczalik Apr 5, 2017
77b950d
kloud/metadata: workaround missing output
rjeczalik Apr 5, 2017
cd8a6ae
kloud/metadata: use embedded screen by default for all distros
rjeczalik Apr 5, 2017
dc97c58
stack/provider: remove unused property - koding_use_embedded
rjeczalik Apr 5, 2017
f082fa7
kloud/metadata: reverse proxy s3 files through koding.com
rjeczalik Apr 5, 2017
140895c
koding: reformat generated files
rjeczalik Apr 5, 2017
b362e95
kloud/metadata: do not overwrite cloud-init output
rjeczalik Apr 5, 2017
e6eb8d0
klient/termina: guess TERM
rjeczalik Apr 5, 2017
633faa4
provider/azure: update tests
rjeczalik Apr 5, 2017
29c5e8c
klient/terminal: add debug logging
rjeczalik Apr 5, 2017
b514f00
klient/terminal: use xterm-color for ubuntu
rjeczalik Apr 5, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/app/lib/providers/templates/aws.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ resource:
aws-instance:
# select your instance_type here: eg. c3.xlarge
instance_type: '${var.userInput_instance_type}'
# select your ami (optional) eg. ami-xxxxx (it should be based on ubuntu 14.04)
# select your ami (defaults to Ubuntu 14.04)
ami: ''
# we will tag the instance here so you can identify it when you login to your AWS console
tags:
Expand Down
2 changes: 1 addition & 1 deletion client/app/lib/providers/templates/vagrant.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ resource:
# define your vm specs here, 2 cpus, 2GB of memory etc.
cpus: '${var.userInput_cpus}'
memory: '${var.userInput_memory}'
# select your image (optional) eg. ubuntu/trusty64 (it should be based on ubuntu 14.04)
# select your image (defaults to ubuntu/trusty64)
box: '${var.userInput_box}'
user_data: |-
# let's create a file on your root folder:
Expand Down
74 changes: 35 additions & 39 deletions go/src/koding/kites/kloud/metadata/cloudinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,60 +177,56 @@ func (ci CloudInit) String() string {
// a kloud's cloud-init that is used to deploy
// a klient service on a remote instance.
type CloudConfig struct {
Username string
Metadata string
Userdata string
KiteKey string
Provision string
Metadata string
Userdata string
}

// NewCloudInit creates new cloud-init content from
// the given configuration.
func NewCloudInit(cfg *CloudConfig) CloudInit {
var files, cmd []interface{}

files = append(files, map[string]interface{}{
"path": "/var/lib/koding/provision.sh",
"permissions": "0755",
"content": provision,
})

cmd = append(cmd, "/var/lib/koding/provision.sh")

if cfg.Metadata != "" {
files = append(files, map[string]interface{}{
"path": "/var/lib/koding/metadata.json",
"content": cfg.Metadata,
"permissions": "0644",
})
}

if cfg.Userdata != "" {
files = append(files, map[string]interface{}{
"path": "/var/lib/koding/user-data.sh",
"permissions": "0755",
"content": cfg.Userdata,
})
}

return CloudInit{
"output": map[string]interface{}{
"all": "| tee -a /var/log/cloud-init-output.log",
},
"users": []interface{}{
"default",
map[string]interface{}{
"name": cfg.Username,
"name": "${var.koding_account_profile_nickname}",
"lock_passwd": bool(true),
"gecos": "Koding",
"groups": []interface{}{
"sudo",
},
"sudo": []interface{}{
"ALL=(ALL) NOPASSWD:ALL",
},
"shell": "/bin/bash",
},
},
"write_files": []interface{}{
map[string]interface{}{
"path": "/etc/kite/kite.key",
"content": cfg.KiteKey,
"groups": []interface{}{"sudo"},
"sudo": []interface{}{"ALL=(ALL) NOPASSWD:ALL"}, // TODO(rjeczalik): limit to only screen + klient
"shell": "/bin/bash",
},
map[string]interface{}{
"path": "/var/lib/koding/metadata.json",
"content": cfg.Metadata,
"permissions": "0644",
},
map[string]interface{}{
"path": "/var/lib/koding/user-data.sh",
"permissions": "0755",
"encoding": "b64",
"content": cfg.Userdata,
},
map[string]interface{}{
"path": "/var/lib/koding/provision.sh",
"permissions": "0755",
"content": cfg.Provision,
},
},
"runcmd": []interface{}{
"/var/lib/koding/provision.sh",
},
"write_files": files,
"runcmd": cmd,
}
}

Expand All @@ -240,7 +236,7 @@ func NewCloudInit(cfg *CloudConfig) CloudInit {
// If p does not look like cloud-init content,
// the function returns the ErrNotCloudInit error.
func ParseCloudInit(p []byte) (CloudInit, error) {
if !bytes.HasPrefix(p, header) {
if !bytes.HasPrefix(bytes.TrimSpace(p), header) {
return nil, ErrNotCloudInit
}

Expand Down
97 changes: 93 additions & 4 deletions go/src/koding/kites/kloud/metadata/metadata.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
package metadata

import "text/template"
import (
"bytes"
"encoding/json"
"errors"
"koding/kites/config"
)

//go:generate $GOPATH/bin/go-bindata -mode 420 -modtime 1470666525 -pkg metadata -o provision.sh.go provision.sh
//go:generate gofmt -l -w -s provision.sh.go

var provision = mustTemplate("provision.sh")
// Default asset URLs used by kloud when deploying klient.
//
// They are being downloaded on a remote vm during provisioning.
//
// TODO(rjeczalik): make it configurable during ./configure = add to kites/config/config.json
var (
DefaultEntrypointBaseURL = "https://koding.com/s3/koding-klient/entrypoint"
DefaultScreenURL = "https://koding.com/d/screen.tar.gz"
DefaultCertURL = "https://koding.com/d/ca-certificates.crt.gz"
)

var provision = string(mustAsset("provision.sh"))

func mustAsset(file string) []byte {
p, err := Asset(file)
Expand All @@ -15,6 +31,79 @@ func mustAsset(file string) []byte {
return p
}

func mustTemplate(file string) *template.Template {
return template.Must(template.New(file).Parse(string(mustAsset(file))))
// Config is used to build cloud-init from user_data
// in order to install klient service on a remote vm
// and authenticate and connect to Koding.
type Config struct {
Konfig *config.Konfig // Koding endpoints configuration
KiteKey string // KiteKey used by klient to authenticate with Koding
Userdata string // User script to run after provisioning, if any
Debug bool // Whether klient should be started in debug mode; configurable by debug field in stack template
}

// New builds new cloud-init for the given configuration.
//
// If cfg.Userdata is a cloud-init file, it will be
// merged back into kloud's one. If there are conflicts
// between the two cloud-init scripts, stack build will
// fail - we do not allow user to overwrite kloud's
// cloud-init.
func New(cfg *Config) (CloudInit, error) {
metadata, err := newMetadata(cfg)
if err != nil {
return nil, errors.New("error building metadata: " + err.Error())
}

ci, err := ParseCloudInit([]byte(cfg.Userdata))
switch err {
case nil:
user := ci
ci = NewCloudInit(&CloudConfig{
Metadata: string(metadata),
})

if err := Merge(user, ci); err != nil {
return nil, errors.New("error merging cloud-init from user-data: " + err.Error())
}

case ErrNotCloudInit:
ci = NewCloudInit(&CloudConfig{
Metadata: string(metadata),
Userdata: cfg.Userdata,
})
default:
return nil, errors.New("error parsing cloud-init from user-data: " + err.Error())
}

return ci, nil
}

// TODO(rjeczalik): refactor to a separate function in kites/config
// and use in marathon as well.
func newMetadata(cfg *Config) ([]byte, error) {
konfig := &config.Konfig{
Endpoints: cfg.Konfig.Endpoints,
KiteKey: cfg.KiteKey,
Debug: cfg.Debug,
}

m := map[string]interface{}{
"konfig.konfig.konfigs": map[string]interface{}{
konfig.ID(): konfig,
},
"konfig.konfig.konfigs.used": map[string]interface{}{
"id": konfig.ID(),
},
}

var buf bytes.Buffer

enc := json.NewEncoder(&buf)
enc.SetEscapeHTML(false)

if err := enc.Encode(m); err != nil {
return nil, err
}

return buf.Bytes(), nil
}
74 changes: 50 additions & 24 deletions go/src/koding/kites/kloud/metadata/provision.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,64 @@
# Copyright (C) 2012-2017 Koding Inc., all rights reserved.

set -euo pipefail

# The following variables are passed via terraform's variable block.
export KODING_USERNAME="$${KODING_USERNAME:-${var.koding_account_profile_nickname}}"
export KLIENT_URL="$${KLIENT_URL:-${var.koding_klient_url}}"
export SCREEN_URL="$${SCREEN_URL:-${var.koding_screen_url}}"

trap "echo _KD_DONE_ | tee -a /var/log/cloud-init-output.log" EXIT

echo "127.0.0.1 {{.Hostname}}" >> /etc/hosts
install_screen() {
curl --location --silent --show-error --retry 5 "$${SCREEN_URL}" --output /tmp/screen.tar.gz
tar -C / -xf /tmp/screen.tar.gz

groupadd --force screen
usermod -a -G screen "$${KODING_USERNAME}"

if [ ! -x /usr/bin/screen ]; then
ln -sf /opt/kite/klient/embedded/bin/screen /usr/bin/screen
fi

if [ ! -e /usr/share/terminfo ]; then
mkdir -p /usr/share
ln -sf /opt/kite/klient/embedded/share/terminfo /usr/share/terminfo
ln -sf /usr/share/terminfo/X /usr/share/terminfo/x
elif ls /usr/share/terminfo/x/xterm* &>/dev/null; then
rm -rf /opt/kite/klient/embedded/share/terminfo
ln -sf /usr/share/terminfo /opt/kite/klient/embedded/share/terminfo
fi

mkdir -p /opt/kite/klient
if [ ! -e /var/run/screen ]; then
mkdir -p /var/run/screen
chmod 0755 /var/run/screen
chown root:screen /var/run/screen
fi
}

wget "{{.KlientURL}}" --retry-connrefused --tries 5 -O /tmp/klient.gz
wget "{{.ScreenURL}}" --retry-connrefused --tries 5 -O /tmp/screen.tar.gz
main() {
echo "127.0.0.1 $${KODING_USERNAME}" >> /etc/hosts

tar -C / -xf /tmp/screen.tar.gz
touch /var/log/klient.log \
/var/log/cloud-init-output.log

if [ ! -x /usr/bin/screen ]; then
ln -sf /opt/kite/klient/embedded/bin/screen /usr/bin/screen
fi
install_screen

if [ ! -e /usr/share/terminfo ]; then
mkdir -p /usr/share
ln -sf /opt/kite/klient/embedded/share/terminfo /usr/share/terminfo
ln -sf /usr/share/terminfo/X /usr/share/terminfo/x
fi
mkdir -p /opt/kite/klient
curl --location --silent --show-error --retry 5 "$${KLIENT_URL}" --output /tmp/klient.gz
gzip --decompress --force --stdout /tmp/klient.gz > /opt/kite/klient/klient
chmod +x /opt/kite/klient/klient

if [ ! -e /var/run/screen ]; then
mkdir -p /var/run/screen
chmod 0755 /var/run/screen
fi
chown -R "$${KODING_USERNAME}" \
/opt/kite/klient \
/var/log/klient.log \
/var/log/cloud-init-output.log

gzip --decompress --force --stdout /tmp/klient.gz > /opt/kite/klient/klient
chmod +x /opt/kite/klient/klient
chown -R "{{.Username}}:{{.Username}}" /opt/kite/klient
/opt/kite/klient/klient -metadata-user "$${KODING_USERNAME}" -metadata-file /var/lib/koding/metadata.json run

/opt/kite/klient/klient -metadata-user "{{.Username}}" -metadata-file /var/lib/koding/metadata.json run
if [ -x /var/lib/koding/user-data.sh ]; then
/var/lib/koding/user-data.sh
fi
}

if [[ -x /var/lib/koding/user-data.sh ]]; then
/var/lib/koding/user-data.sh
fi
main &>>/var/log/cloud-init-output.log
4 changes: 2 additions & 2 deletions go/src/koding/kites/kloud/metadata/provision.sh.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 1 addition & 6 deletions go/src/koding/kites/kloud/provider/aws/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,10 @@ func (s *Stack) ApplyTemplate(c *stack.Credential) (*stack.Template, error) {
instance["security_groups"] = []string{bootstrap.SG}
}

ud, err := s.BuildUserdata(resourceName, instance)
if err != nil {
if err := s.BuildUserdata(resourceName, instance); err != nil {
return nil, err
}

t.Variable[ud.KiteKeyName] = map[string]interface{}{
"default": ud.KiteKeys,
}

resource.AwsInstance[resourceName] = instance
}

Expand Down
17 changes: 1 addition & 16 deletions go/src/koding/kites/kloud/provider/azure/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func (s *Stack) ApplyTemplate(c *stack.Credential) (*stack.Template, error) {

s.injectEndpointRules(vm)

if err := s.injectCloudInit(name, vm); err != nil {
if err := s.BuildUserdata(name, vm); err != nil {
return nil, err
}

Expand Down Expand Up @@ -308,21 +308,6 @@ func (s *Stack) injectEndpointRules(vm map[string]interface{}) {
vm["endpoint"] = endpoints
}

func (s *Stack) injectCloudInit(name string, vm map[string]interface{}) error {
ub, err := s.BuildUserdata(name, vm)
if err != nil {
return err
}

s.Debug = ub.Debug

s.Builder.Template.Variable[ub.KiteKeyName] = map[string]interface{}{
"default": ub.KiteKeys,
}

return nil
}

func newBootstrapTmpl(cfg *BootstrapConfig) ([]byte, error) {
var buf bytes.Buffer

Expand Down
Loading