Skip to content

Commit

Permalink
Add support for cloud-init data sources (aws, etc)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuildthecloud committed May 4, 2019
1 parent ae17952 commit 016bd8c
Show file tree
Hide file tree
Showing 18 changed files with 282 additions and 35 deletions.
18 changes: 17 additions & 1 deletion cmd/ccapply/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package main

import (
"encoding/json"
"fmt"
"os"

"github.com/rancher/k3os/pkg/cc"
"github.com/rancher/k3os/pkg/config"

"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
Expand All @@ -16,6 +16,8 @@ var (
bootPhase = false
configPhase = false
debug = false
dump = false
dumpJSON = false
)

func main() {
Expand Down Expand Up @@ -44,6 +46,16 @@ func main() {
Destination: &configPhase,
Usage: "Run os-config stage",
},
cli.BoolFlag{
Name: "dump",
Destination: &dump,
Usage: "Print current configuration",
},
cli.BoolFlag{
Name: "dump-json",
Destination: &dumpJSON,
Usage: "Print current configuration in json",
},
}
if err := app.Run(os.Args); err != nil {
logrus.Fatal(err)
Expand Down Expand Up @@ -77,6 +89,10 @@ func doRun() error {
return cc.BootApply(&cfg)
} else if configPhase {
return cc.ConfigApply(&cfg)
} else if dump {
return config.Write(cfg, os.Stdout)
} else if dumpJSON {
return json.NewEncoder(os.Stdout).Encode(&cfg)
}

return cc.RunApply(&cfg)
Expand Down
8 changes: 0 additions & 8 deletions images/00-base/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ RUN apk -U add \
iptables \
jq \
kbd-bkeymaps \
libseccomp \
logrotate \
nfs-utils \
open-iscsi \
Expand All @@ -33,18 +32,11 @@ RUN apk -U add \
procps \
rsync \
strace \
squashfs-tools \
sudo \
tar \
util-linux \
vim \
wget \
xz
RUN if [ "$ARCH" == "amd64" ]; then \
apk add open-vm-tools grub-bios \
;fi
RUN cp /etc/apk/repositories /etc/apk/repositories.orig && \
echo 'http://dl-3.alpinelinux.org/alpine/edge/testing' >> /etc/apk/repositories && \
apk -U add efibootmgr && \
mv /etc/apk/repositories.orig /etc/apk/repositories && \
apk update
2 changes: 2 additions & 0 deletions images/02-progs/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ RUN mkdir -p /output && \
WORKDIR $GOPATH/src/github.com/linuxkit/linuxkit
RUN cd pkg/init/cmd/rc.init && \
gobuild -o /output/rc.init
RUN cd pkg/metadata && \
gobuild -o /output/metadata

FROM gobuild as k3os
COPY /cmd/ $GOPATH/src/github.com/rancher/k3os/cmd/
Expand Down
18 changes: 18 additions & 0 deletions images/02-rootfs/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ FROM ${REPO}/k3os-k3s:${TAG} as k3s
FROM base as k3os-build
ARG VERSION
ARG ARCH
RUN apk add squashfs-tools
COPY --from=base /bin /usr/src/image/bin/
COPY --from=base /lib /usr/src/image/lib/
COPY --from=base /sbin /usr/src/image/sbin/
Expand Down Expand Up @@ -59,10 +60,27 @@ RUN rm -rf \
mkdir -p /usr/src/image/etc/ssl/certs/ && \
cp -rf /etc/ssl/certs/ca-certificates.crt /usr/src/image/etc/ssl/certs

RUN rm -rf \
/usr/src/image/usr/sbin/apk \
/usr/src/image/usr/include \
/usr/src/image/usr/lib/apk \
/usr/src/image/usr/lib/pkgconfig \
/usr/src/image/usr/lib/systemd \
/usr/src/image/usr/lib/udev \
/usr/src/image/usr/share/apk \
/usr/src/image/usr/share/applications \
/usr/src/image/usr/share/ca-certificates \
/usr/src/image/usr/share/icons \
/usr/src/image/usr/share/mkinitfs \
/usr/src/image/usr/share/vim/vim81/spell \
/usr/src/image/usr/share/vim/vim81/tutor \
/usr/src/image/usr/share/vim/vim81/doc

COPY --from=k3s /output/install.sh /usr/src/image/libexec/k3os/k3s-install.sh
COPY --from=progs /output/rc.init /usr/src/image/bin/rc.init
COPY --from=progs /output/k3os-config /usr/src/image/libexec/k3os/k3os-config
COPY --from=progs /output/ccapply /usr/src/image/sbin/ccapply
COPY --from=progs /output/metadata /usr/src/image/sbin/metadata

COPY overlay/ /usr/src/image/
COPY install.sh /usr/src/image/libexec/k3os/install
Expand Down
2 changes: 0 additions & 2 deletions images/06-iso/grub.cfg
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
set default=0
set timeout=10

loadfont "unicode"
set gfxmode=auto
set gfxpayload=keep
insmod all_video
insmod gfxterm
terminal_output gfxterm

menuentry "k3OS LiveCD & Installer" {
search.fs_label K3OS root
Expand Down
4 changes: 1 addition & 3 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,10 @@ install_grub()
set default=0
set timeout=10
loadfont "unicode"
set gfxmode=auto
set gfxpayload=keep
insmod all_video
insmod gfxterm
terminal_output gfxterm
menuentry "k3OS Current" {
search.fs_label K3OS_STATE root
Expand Down Expand Up @@ -297,7 +295,7 @@ fi
if [ "$K3OS_INSTALL_POWER_OFF" = true ] || grep -q 'k3os.mode=install' /proc/cmdline; then
poweroff -f
else
echo " * Rebooting system in 5 seconds"
echo " * Rebooting system in 5 seconds (CTRL+C to cancel)"
sleep 5
reboot -f
fi
1 change: 1 addition & 0 deletions overlay/etc/conf.d/connman
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
command_args="-r"
9 changes: 9 additions & 0 deletions overlay/etc/init.d/cloud-config
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/sbin/openrc-run

depend() {
after net-online
need net
}

name="cloud-config"
command="/usr/sbin/metadata"
6 changes: 5 additions & 1 deletion overlay/libexec/k3os/boot
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ setup_services()
ln -s /etc/init.d/$i /etc/runlevels/boot
done

if [ -e /etc/conf.d/cloud-config ]; then
ln -s /etc/init.d/cloud-config /etc/runlevels/boot/
fi

for i in sshd k3s "local" ccapply iscsid; do
ln -s /etc/init.d/$i /etc/runlevels/default
done
Expand Down Expand Up @@ -163,6 +167,6 @@ setup_shell
setup_root
setup_inittab
setup_sudoers
setup_services
ccapply --boot
setup_services
cleanup
7 changes: 3 additions & 4 deletions pkg/cc/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func runApplies(cfg *config.CloudConfig, appliers ...applier) error {
func RunApply(cfg *config.CloudConfig) error {
return runApplies(cfg,
ApplySSHKeysWithNet,
//ApplyMounts,
ApplyWriteFiles,
ApplyRuncmd,
ApplyInstall,
Expand All @@ -43,12 +42,13 @@ func ConfigApply(cfg *config.CloudConfig) error {

func BootApply(cfg *config.CloudConfig) error {
return runApplies(cfg,
ApplyDataSource,
ApplyModules,
ApplySysctls,
ApplyHostname,
//ApplyDNS,
ApplyDNS,
ApplyWifi,
ApplyPassword,
//ApplyMounts,
ApplySSHKeys,
ApplyK3SNoRestart,
ApplyWriteFiles,
Expand All @@ -61,7 +61,6 @@ func InitApply(cfg *config.CloudConfig) error {
ApplyModules,
ApplySysctls,
ApplyHostname,
//ApplyDNS,
ApplyWriteFiles,
ApplyInitcmd,
)
Expand Down
80 changes: 80 additions & 0 deletions pkg/cc/funcs.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cc

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"sort"
Expand Down Expand Up @@ -168,3 +170,81 @@ func ApplyInstall(cfg *config.CloudConfig) error {
cmd.Stdin = os.Stdin
return cmd.Run()
}

func ApplyDNS(cfg *config.CloudConfig) error {
buf := &bytes.Buffer{}
buf.WriteString("NetworkInterfaceBlacklist=veth\n")
if len(cfg.K3OS.DNSNameservers) > 0 {
dns := strings.Join(cfg.K3OS.DNSNameservers, ",")
buf.WriteString("FallbackNameservers=")
buf.WriteString(dns)
buf.WriteString("\n")
} else {
buf.WriteString("FallbackNameservers=8.8.8.8\n")
}

if len(cfg.K3OS.NTPServers) > 0 {
ntp := strings.Join(cfg.K3OS.NTPServers, ",")
buf.WriteString("FallbackTimeservers=")
buf.WriteString(ntp)
buf.WriteString("\n")
}

err := ioutil.WriteFile("/etc/connman/main.conf ", buf.Bytes(), 0644)
if err != nil {
return fmt.Errorf("failed to write /etc/connman/main.conf: %v", err)
}

return nil
}

func ApplyWifi(cfg *config.CloudConfig) error {
if len(cfg.K3OS.Wifi) == 0 {
return nil
}

buf := &bytes.Buffer{}

buf.WriteString("[global]\n")
buf.WriteString("Name=cloud-config\n")
buf.WriteString("Description=Services defined in the cloud-config\n")

for i, w := range cfg.K3OS.Wifi {
name := fmt.Sprintf("wifi%d", i)
buf.WriteString("[service_")
buf.WriteString(name)
buf.WriteString("]\n")
buf.WriteString("Type=wifi\n")
buf.WriteString("Passphrase=")
buf.WriteString(w.Passphrase)
buf.WriteString("\n")
buf.WriteString("SSID=")
buf.WriteString(w.SSID)
buf.WriteString("\n")
}

if buf.Len() > 0 {
return ioutil.WriteFile("/var/lib/connman/cloud-config.config", buf.Bytes(), 0644)
}

return nil
}

func ApplyDataSource(cfg *config.CloudConfig) error {
if len(cfg.K3OS.DataSources) == 0 {
return nil
}

args := strings.Join(cfg.K3OS.DataSources, " ")
buf := &bytes.Buffer{}

buf.WriteString("command_args=\"")
buf.WriteString(args)
buf.WriteString("\"\n")

if err := ioutil.WriteFile("/etc/conf.d/cloud-config", buf.Bytes(), 0644); err != nil {
return fmt.Errorf("failed to write to /etc/conf.d/cloud-config: %v", err)
}

return nil
}
2 changes: 1 addition & 1 deletion pkg/cliinstall/ask.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func isInstall(cfg *config.CloudConfig) (bool, error) {
return false, nil
}

i, err := questions.PromptFormattedOptions("Choose operation", -1,
i, err := questions.PromptFormattedOptions("Choose operation", 0,
"Install to disk",
"Configure server or agent")
if err != nil {
Expand Down
16 changes: 12 additions & 4 deletions pkg/config/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ var (
readSystemConfig,
readCmdline,
readLocalConfig,
readCloudConfig,
readUserData,
}
)

Expand Down Expand Up @@ -63,13 +65,17 @@ func mapToEnv(prefix string, data map[string]interface{}) []string {
}

func ReadConfig() (CloudConfig, error) {
return readersToObject(append(readers, readLocalConfigs()...)...)
}

func readersToObject(readers ...reader) (CloudConfig, error) {
result := CloudConfig{
K3OS: K3OS{
Install: &Install{},
},
}

data, err := merge(append(readers, readLocalConfigs()...)...)
data, err := merge(readers...)
if err != nil {
return result, err
}
Expand All @@ -86,7 +92,9 @@ func merge(readers ...reader) (map[string]interface{}, error) {
if err != nil {
return nil, err
}

if err := schema.Mapper.ToInternal(newData); err != nil {
return nil, err
}
data = merge2.UpdateMerge(schema, schemas, data, newData, false)
}
return data, nil
Expand Down Expand Up @@ -137,7 +145,7 @@ func readFile(path string) (map[string]interface{}, error) {
return nil, err
}

return data, schema.Mapper.ToInternal(data)
return data, nil
}

func readCmdline() (map[string]interface{}, error) {
Expand Down Expand Up @@ -169,5 +177,5 @@ func readCmdline() (map[string]interface{}, error) {
}
}

return data, schema.Mapper.ToInternal(data)
return data, nil
}
Loading

0 comments on commit 016bd8c

Please sign in to comment.