harbr 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 78fa473f8be877a6b04976d3b3642a6bbf87e7c1a172fc507db1cab6b1e12285
4
- data.tar.gz: 3fea9186e4dc9638f6c96f9a8c69e45b98854a58803e064ed9014b4aaa452f63
3
+ metadata.gz: 5478cb2b7a5748ff7d5fd59838e372c5824b7ffcbf94573b9bfdb920a390d525
4
+ data.tar.gz: 8f1f2d87902fc7e3476f37b86cb3979ec290e232cbc5bb6a0cb16540813d02a8
5
5
  SHA512:
6
- metadata.gz: a36901e43e4ad639993e14691a2abac6a50d6d31d88b4d3e817b701a8a3f46f6300b311e5d42ec6e84369a28c655a587528f7967b65f8fcb3a4fc494304df0ee
7
- data.tar.gz: fbdd20b24e79d60b64520639ae31f48e4d92fb1fc6232beefeec811a2297f6e8c5afefc6dd77ce5beedb277a3c62e0ed9ec04a55cc1552079450d66cf97456f7
6
+ metadata.gz: ac26ca50fb01be84db510f901180219ed84999da405d12196d407c4ff61fdef9e34f453017b50f3070379419daa365f21bd21826307a11b4c349e04ecf8bbf84
7
+ data.tar.gz: 11d0a464b3d3becf3823984cad65f2dd861b2ac25ddb83dbed0e44820cb03f12004077d5d1fe39176d6638166b6f32a4bf2169f3914dfbb7d9230ae839e8233f
data/exe/harbr CHANGED
@@ -80,7 +80,6 @@ class HarbrCLI < Thor
80
80
  File.write("/etc/traefik/harbr.toml", TomlRB.dump(config))
81
81
  puts "Traefik configuration written to /etc/traefik/harbr.toml"
82
82
  end
83
-
84
83
  end
85
84
 
86
85
  desc "destroy", "Destroy a container and remove all traces"
@@ -148,8 +147,7 @@ class HarbrCLI < Thor
148
147
  desc "deploy", "deploy a container to production"
149
148
  method_option :live, type: :boolean, default: false, aliases: "-l", desc: "deploy to live environment"
150
149
  method_option :next, type: :boolean, default: false, aliases: "-n", desc: "deploy to next environment"
151
- def deploy(name,version=nil)
152
-
150
+ def deploy(name, version = nil)
153
151
  if options[:live]
154
152
  /versions\/(?<version>\d*)/ =~ `ls -l /var/harbr/containers/#{name}/next`
155
153
  version = $1
@@ -166,11 +164,35 @@ class HarbrCLI < Thor
166
164
  if options[:next]
167
165
  `ln -sf /var/harbr/containers/#{name}/version/#{version} /var/harbr/containers/#{name}/next`
168
166
  Harbr::Job.perform_async(name, version, "next")
169
- puts "deploy version #{version} of #{name} to next environment"
167
+ puts "deploy version #{version} of #{name} to next environment"
168
+ end
169
+ end
170
+
171
+ desc "containerise", "deploy a container to a lxd comtainer"
172
+ method_option :live, type: :boolean, default: false, aliases: "-l", desc: "deploy to live environment"
173
+ method_option :next, type: :boolean, default: false, aliases: "-n", desc: "deploy to next environment"
174
+ def containerise(name, version = nil)
175
+ if options[:live]
176
+ /versions\/(?<version>\d*)/ =~ `ls -l /var/harbr/containers/#{name}/next`
177
+ version = $1
178
+ raise "Ooops! next version not found!" if version.nil?
179
+
180
+ Harbr::Lxd::Job.perform_async(name, 9292, "live")
181
+
182
+ /versions\/(?<version>\d*)/ =~ `ls -l /var/harbr/containers/#{name}/live`
183
+ `ln -sf /var/harbr/containers/#{name}/version/#{$1} /var/harbr/containers/#{name}/rollback`
184
+
185
+ puts "deploy version #{version} of #{name} to live environment"
186
+ end
187
+
188
+ if options[:next]
189
+ `ln -sf /var/harbr/containers/#{name}/version/#{version} /var/harbr/containers/#{name}/next`
190
+ Harbr::Joarbr::Lxd::Job.perform_async(name, 9292, "next")
191
+ puts "deploy version #{version} of #{name} to next environment"
170
192
  end
171
-
172
193
  end
173
194
 
195
+
174
196
  desc "rollback", "rollback last deploy"
175
197
  def rollback(name)
176
198
  Dir.chdir("/var/harbr/containers/#{name}") do
@@ -190,7 +212,6 @@ class HarbrCLI < Thor
190
212
  def update
191
213
  system "gem update harbr"
192
214
  end
193
-
194
215
  end
195
216
 
196
217
  HarbrCLI.start(ARGV)
@@ -0,0 +1,119 @@
1
+ require "fileutils"
2
+
3
+ module Harbr
4
+ module Runit
5
+ class Script
6
+ def initialize(container, port, env)
7
+ @container_name = container
8
+ @port = port
9
+ @env = env
10
+ end
11
+
12
+ def run_script
13
+ <<~SCRIPT
14
+ #!/bin/sh
15
+ exec 2>&1
16
+ cd /var/harbr/containers/#{@container_name}/#{@env}
17
+ exec ./exe/run #{@port} #{@env}
18
+ echo "started #{@container_name} on port #{@port}"
19
+ SCRIPT
20
+ end
21
+
22
+ def finish_script
23
+ <<~SCRIPT
24
+ #!/bin/sh
25
+ lsof -i :#{@port} | awk 'NR!=1 {print $2}' | xargs kill
26
+ echo "killed #{@container_name} on port #{@port}"
27
+ SCRIPT
28
+ end
29
+
30
+ def log_script
31
+ <<~SCRIPT
32
+ #!/bin/sh
33
+ echo "starting log for #{@container_name} on port #{@port}"
34
+ exec svlogd -tt /var/log/harbr/#{@container_name}/#{@env}
35
+ SCRIPT
36
+ end
37
+ end
38
+ end
39
+ module Lxd
40
+ class Job
41
+ include SuckerPunch::Job
42
+
43
+ def perform(name, version, env, port)
44
+ ip_address = lxd_pack(name, env, port)
45
+ puts "Container IP Address: #{ip_address}"
46
+ end
47
+
48
+ private
49
+
50
+ def lxd_pack(name, env, port)
51
+ base_path = "/var/harbr/containers/paiddm"
52
+ source_path = File.join(base_path, env)
53
+ runit_script = Runit::Script.new(name, port, env)
54
+
55
+ # Check if the source path exists
56
+ raise "Source path #{source_path} does not exist." unless File.directory?(source_path)
57
+
58
+ # Create the container
59
+ system("lxc launch ubuntu:20.04 #{name}")
60
+ sleep(5) # Wait for the container to initialize
61
+
62
+ # Check if runit is installed, if not then install
63
+ unless system("lxc exec #{name} -- dpkg -s runit")
64
+ system("lxc exec #{name} -- apt-get update")
65
+ system("lxc exec #{name} -- apt-get install -y runit")
66
+ end
67
+
68
+ # Check if chruby is installed, if not then install chruby, ruby-install and Ruby versions
69
+ unless system("lxc exec #{name} -- bash -c 'type chruby'")
70
+ system("lxc exec #{name} -- apt-get install -y git curl build-essential zlib1g-dev libssl-dev libreadline-dev libyaml-dev libgdbm-dev libncurses5-dev libffi-dev")
71
+ system("lxc exec #{name} -- wget -O chruby-0.3.9.tar.gz https://github.com/postmodern/chruby/archive/v0.3.9.tar.gz")
72
+ system("lxc exec #{name} -- tar -xzvf chruby-0.3.9.tar.gz")
73
+ system("lxc exec #{name} -- cd chruby-0.3.9/ && make install")
74
+ system("lxc exec #{name} -- echo 'source /usr/local/share/chruby/chruby.sh' >> ~/.bashrc")
75
+ system("lxc exec #{name} -- wget -O ruby-install-0.8.3.tar.gz https://github.com/postmodern/ruby-install/archive/v0.8.3.tar.gz")
76
+ system("lxc exec #{name} -- tar -xzvf ruby-install-0.8.3.tar.gz")
77
+ system("lxc exec #{name} -- cd ruby-install-0.8.3/ && make install")
78
+
79
+ # Install Ruby versions
80
+ ["3.2.2", "3.1.2", "3.3.0"].each do |ruby_version|
81
+ system("lxc exec #{name} -- ruby-install ruby #{ruby_version}")
82
+ end
83
+ end
84
+ # Copy the entire directory structure to the container
85
+ system("lxc file push -r #{source_path}/* #{name}/var/harbr/containers/#{name}/")
86
+
87
+ # Start the container, if it's not already running
88
+ system("lxc start #{name}")
89
+ sleep(5) # Wait for the container to fully start
90
+
91
+ # Create Runit scripts and directories
92
+ sv_path = "/etc/sv/#{name}"
93
+ system("lxc exec #{name} -- mkdir -p #{sv_path}")
94
+ system("lxc exec #{name} -- mkdir -p /var/log/harbr/#{name}/#{env}")
95
+
96
+ # Create and set execute permissions on each script
97
+ ["run_script", "finish_script", "log_script"].each do |script_method|
98
+ script_content = runit_script.send(script_method)
99
+ File.write("tmp_script.sh", script_content)
100
+ system("lxc file push tmp_script.sh #{name}#{sv_path}/#{script_method.gsub("_script", "")}")
101
+ system("lxc exec #{name} -- chmod +x #{sv_path}/#{script_method.gsub("_script", "")}")
102
+ File.delete("tmp_script.sh")
103
+ end
104
+
105
+ # Symlink from source to /etc/sv
106
+ system("lxc exec #{name} -- ln -s #{source_path} /etc/sv/#{name}")
107
+
108
+ # Symlink from sv to /etc/service
109
+ system("lxc exec #{name} -- ln -s #{sv_path} /etc/service/#{name}")
110
+
111
+ # Fetch the container's IP address
112
+ ip_address = `lxc list #{name} -c 4 --format csv`.strip
113
+
114
+ puts "Container #{name} created on #{ip_address}, Runit scripts set up, and started."
115
+ ip_address
116
+ end
117
+ end
118
+ end
119
+ end
data/lib/harbr/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Harbr
4
- VERSION = "0.2.2"
4
+ VERSION = "0.2.3"
5
5
  end
data/lib/harbr.rb CHANGED
@@ -20,7 +20,7 @@ module Harbr
20
20
  Dddr.configure do |config|
21
21
  config.data_dir = DEFAULT_DIRECTORY_DATA_DIR
22
22
  end
23
-
23
+
24
24
  class Error < StandardError; end
25
25
 
26
26
  def self.send_notification(subject, body)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: harbr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Delaney Kuldvee Burke
@@ -150,6 +150,7 @@ files:
150
150
  - lib/harbr/.DS_Store
151
151
  - lib/harbr/container.rb
152
152
  - lib/harbr/job.rb
153
+ - lib/harbr/lxd/job.rb
153
154
  - lib/harbr/version.rb
154
155
  - sig/harbr.rbs
155
156
  - vendor/bundle/ruby/3.2.0/bin/bundle