Class: Fig2CoreOS

Inherits:
Object
  • Object
show all
Defined in:
lib/fig2coreos.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_name, fig_file, output_dir, options = {}) ⇒ Fig2CoreOS

Returns a new instance of Fig2CoreOS.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fig2coreos.rb', line 12

def initialize(app_name, fig_file, output_dir, options={})
  @app_name = app_name
  @fig = YAML.load_file(fig_file.to_s)
  @output_dir = File.expand_path(output_dir.to_s)

  # clean and setup directory structure
  FileUtils.rm_rf(File.join(@output_dir, "media"))
  FileUtils.rm_rf(File.join(@output_dir, "setup-coreos.sh"))
  FileUtils.rm_rf(File.join(@output_dir, "Vagrantfile"))
  FileUtils.mkdir_p(File.join(@output_dir, "media", "state", "units"))

  create_service_files

  create_vagrant_file if options[:type] == "vagrant"

  exit 0
end

Class Method Details

.convert(app_name, fig_file, output_dir, options = {}) ⇒ Object



8
9
10
# File 'lib/fig2coreos.rb', line 8

def self.convert(app_name, fig_file, output_dir, options={})
  Fig2CoreOS.new(app_name, fig_file, output_dir, options)
end

Instance Method Details

#create_service_filesObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/fig2coreos.rb', line 30

def create_service_files
	@fig.each do |service_name, service|
    image = service["image"]
    ports = (service["ports"] || []).map{|port| "-p #{port}"}
    volumes = (service["volumes"] || []).map{|volume| "-v #{volume}"}
    links = (service["links"] || []).map{|link| "--link #{link}_1:#{link}_1"}
    envs = (service["environment"] || []).map do |env_name, env_value|
      "-e \"#{env_name}=#{env_value}\""
    end

    after = if service["links"]
      "#{service["links"].last}.1"
    else
      "docker"
    end

		File.open(File.join(@output_dir, "media", "state", "units", "#{service_name}.1.service"), "w") do |file|
      file << <<-eof
[Unit]
Description=Run #{service_name}_1
After=#{after}.service
Requires=#{after}.service

[Service]
Restart=always
RestartSec=10s
ExecStartPre=/usr/bin/docker ps -a -q | xargs docker rm
ExecStart=/usr/bin/docker run -rm -name #{service_name}_1 #{volumes.join(" ")} #{links.join(" ")} #{envs.join(" ")} #{ports.join(" ")} #{image}
ExecStartPost=/usr/bin/docker ps -a -q | xargs docker rm
ExecStop=/usr/bin/docker kill #{service_name}_1
ExecStopPost=/usr/bin/docker ps -a -q | xargs docker rm

[Install]
WantedBy=local.target
eof
		end

    File.open(File.join(@output_dir, "media", "state", "units", "#{service_name}-discovery.1.service"), "w") do |file|
      file << <<-eof
[Unit]
Description=Announce #{service_name}_1
BindsTo=#{service_name}.1.service

[Service]
ExecStart=/bin/sh -c "while true; do etcdctl set /services/#{service_name}/#{service_name}_1 '{ \\"host\\": \\"%H\\", \\"port\\": #{service["ports"].first.to_s.split(":").first}, \\"version\\": \\"52c7248a14\\" }' --ttl 60;sleep 45;done"
ExecStop=/usr/bin/etcdctl rm /services/#{service_name}/#{service_name}_1

[X-Fleet]
X-ConditionMachineOf=#{service_name}.1.service
eof
    end
  end
end

#create_vagrant_fileObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/fig2coreos.rb', line 84

def create_vagrant_file
  File.open(File.join(@output_dir, "setup-coreos.sh"), "w") do |file|
    file << <<-eof
# Switch to root for setting up systemd
sudo -i

# Clear old containers
systemctl stop local-enable.service
systemctl stop etcd-cluster.service
/usr/bin/docker ps -a -q | xargs docker kill
/usr/bin/docker ps -a -q | xargs docker rm

# Copy the services into place
eof
    @fig.each do |service_name, service|
      file << "cp " + File.join("share", "media", "state", "units", "#{service_name}.1.service") + " /media/state/units/#{service_name}.1.service\n"
      file << "cp " + File.join("share", "media", "state", "units", "#{service_name}-discovery.1.service") + " /media/state/units/#{service_name}-discovery.1.service\n\n"
    end

    file << <<-eof

# Fix etcd-cluster setup
HOSTNAME=$(</proc/sys/kernel/hostname)

if [ ! -d /var/run/etcd/${HOSTNAME} ]
then
mkdir /var/run/etcd/${HOSTNAME}
chown core:core /var/run/etcd/${HOSTNAME}
fi

echo 192.168.10.2 > /var/run/etcd/MY_IP

# Replace the existing line:
sed -i -e "s@ExecStart=.*@ExecStart=/usr/bin/etcd -s 192.168.10.2:7001 -sl 0.0.0.0 -cl 0.0.0.0 -c 192.168.10.2:4001 ${CLUSTER_OPT} -d /var/run/etcd/${HOSTNAME} -n $HOSTNAME@" /media/state/units/etcd-cluster.service

# Start containers and fleet
systemctl daemon-reload
systemctl start etcd-cluster.service
systemctl start local-enable.service
systemctl start fleet

etcdctl mkdir /services
eof
    @fig.each do |service_name, service|
      file << "etcdctl mkdir /services/#{service_name}\n"
    end
    file << "cd /media/state/units; for service in *.service; do fleetctl start $service; done\n"
    file << "sleep 3; /usr/bin/docker ps -a -q | xargs docker rm\n"
    file << "sleep 3; /usr/bin/docker ps -a -q | xargs docker rm\n"
    file << "echo 'SUCCESS'\n"
    file << "exit 0\n"

    file.chmod(0755)
  end

  File.open(File.join(@output_dir, "Vagrantfile"), "w") do |file|
    file << <<-eof
# -*- mode: ruby -*-
# vi: set ft=ruby :

$expose_port = 8080

Vagrant.configure("2") do |config|
config.vm.box = "coreos"
config.vm.box_url = "http://storage.core-os.net/coreos/amd64-generic/dev-channel/coreos_production_vagrant.box"
config.vm.hostname = "coreos-#{@app_name}"

config.vm.provider :virtualbox do |vb, override|
  vb.name = "vagrant-coreos-docker-converted-from-fig-#{@app_name}"
  # Fix docker not being able to resolve private registry in VirtualBox
  vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
  vb.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
end

# Share this folder so provisioner can access Dockerfile an apache.service
# This is from coreos/coreos-vagrant, but won't work on Windows hosts
config.vm.network "private_network", ip: "192.168.10.2"
config.vm.synced_folder ".", "/home/core/share", id: "core", :nfs => true,  :mount_options   => ['nolock,vers=3,udp']

# Forward port 80 from coreos (which is forwarding port 80 of the container)
config.vm.network :forwarded_port, guest: 80, host: $expose_port, host_ip: "127.0.0.1"

config.vm.provider :vmware_fusion do |vb, override|
  override.vm.box_url = "http://storage.core-os.net/coreos/amd64-generic/dev-channel/coreos_production_vagrant_vmware_fusion.box"
end

# plugin conflict
if Vagrant.has_plugin?("vagrant-vbguest") then
  config.vbguest.auto_update = false
end

# Provision
config.vm.provision "shell", :path => "setup-coreos.sh"
end
eof
  end

  if File.directory?(File.join(@output_dir, ".vagrant"))
    puts "[SUCCESS] Try this: cd #{@output_dir} && vagrant reload --provision"
  else
    puts "[SUCCESS] Try this: cd #{@output_dir} && vagrant up"
  end
end