prepper 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d68d0ee338e47bb82505f4b37da819ea323e77de3f3e49cba9bbec6d4d5f6d04
4
+ data.tar.gz: 32e04ba18f7dbaf9397ee08a3dfb6a72e18026fb54b157c3b3649ce1ab792f54
5
+ SHA512:
6
+ metadata.gz: f71e24560ab3572628fdc97e663da08142af71ef848ac096dc988d37b0bf2c3c7abe712520b4a2a418d7d29d5676b3ff6a27577a8cc559b5fd3151c6ca220ec2
7
+ data.tar.gz: ecef0450a23ff26eef8ea203638c3b9ca22f19cbd9648795246d7cdb242e9be0fcf4378fb91a0e68535ede0c9b2e2117509e7e84c4711defbc7c127fb728709a
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.2
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at molnargerg@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in prepper.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "minitest", "~> 5.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ prepper (0.1.0)
5
+ sshkit
6
+ tty-option
7
+ zeitwerk
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ byebug (11.1.3)
13
+ minitest (5.16.3)
14
+ net-scp (4.0.0)
15
+ net-ssh (>= 2.6.5, < 8.0.0)
16
+ net-ssh (7.0.1)
17
+ rake (12.3.3)
18
+ sshkit (1.21.3)
19
+ net-scp (>= 1.1.2)
20
+ net-ssh (>= 2.8.0)
21
+ tty-option (0.2.0)
22
+ zeitwerk (2.6.6)
23
+
24
+ PLATFORMS
25
+ ruby
26
+
27
+ DEPENDENCIES
28
+ byebug
29
+ minitest (~> 5.0)
30
+ prepper!
31
+ rake (~> 12.0)
32
+
33
+ BUNDLED WITH
34
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Greg Molnar
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # Prepper
2
+
3
+ Prepper is a simple server provisioning tool, built on top of SSHKit. You can
4
+ use it to script your server build process.
5
+
6
+
7
+ ## Installation
8
+
9
+ $ gem install prepper
10
+
11
+ ## Usage
12
+
13
+ Prepper works with "packages". You define a package with a name and pass it a block.
14
+ Within that block you can execute commands on the target host.
15
+ There are built in helpers to install `apt` packages, manage directories and
16
+ upload files to the server, etc.
17
+
18
+ A simple example:
19
+
20
+ ```
21
+ server_host "YOUR_SERVER_IP"
22
+ server_port 22
23
+ server_user "root"
24
+
25
+ # let's install the necessary packages to run a Rails app with Postgresql
26
+ package :apt do
27
+ apt_update
28
+ apt_install %w(git-core build-essential libcurl4 libcurl4-openssl-dev libjemalloc-dev postgresql-client libpq-dev postgresql-contrib)
29
+ end
30
+
31
+ # now we will add a deploy user
32
+ package :add_deploy_user do
33
+ add_user 'deploy', shell: '/bin/bash', flags: '--disabled-password'
34
+
35
+ directory '/home/deploy/.ssh', owner: 'deploy:deploy'
36
+ file '/home/deploy/.ssh/authorized_keys', owner: 'deploy:deploy', mode: '655', content: 'ssh-rsa YOUR PUBLIC SSH KEY'
37
+ file '/etc/sudoers.d/deploy', owner: 'root:root', template: 'sudoers'
38
+ end
39
+
40
+ # install rbenv and Ruby 3.1.2
41
+ package :install_ruby do
42
+ install_rbenv 'deploy'
43
+ install_ruby 'deploy', '3.1.2', '--with-jemalloc'
44
+ end
45
+
46
+ ```
47
+
48
+ You can see a full example in [examples/config.rb](examples/config.rb). You would run that file with `bundle exec prepper config.rb` to provision the server.
49
+
50
+ ## Development
51
+
52
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
53
+
54
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
55
+
56
+ ## Contributing
57
+
58
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/prepper. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/prepper/blob/master/CODE_OF_CONDUCT.md).
59
+
60
+
61
+ ## License
62
+
63
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
64
+
65
+ ## Code of Conduct
66
+
67
+ Everyone interacting in the Prepper project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/prepper/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "prepper"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/prepper ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift "#{File.expand_path(File.dirname(__FILE__))}/../lib"
3
+ $VERBOSE = nil
4
+ require 'prepper'
5
+
6
+ Prepper::Cli.new.parse.run
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,91 @@
1
+ server_host "YOUR_SERVER_IP"
2
+ server_port 22
3
+ server_user "root"
4
+
5
+ # let's install the necessary packages to run a Rails app with Postgresql
6
+ package :apt do
7
+ apt_update
8
+ apt_install %w(git-core build-essential libcurl4 libcurl4-openssl-dev libjemalloc-dev postgresql-client libpq-dev postgresql-contrib)
9
+ end
10
+
11
+ # now we will add a deploy user
12
+ package :add_deploy_user do
13
+ add_user 'deploy', shell: '/bin/bash', flags: '--disabled-password'
14
+
15
+ directory '/home/deploy/.ssh', owner: 'deploy:deploy'
16
+ file '/home/deploy/.ssh/authorized_keys', owner: 'deploy:deploy', mode: '655', content: 'ssh-rsa YOUR PUBLIC SSH KEY'
17
+ file '/etc/sudoers.d/deploy', owner: 'root:root', template: 'sudoers'
18
+ end
19
+
20
+ # install rbenv and Ruby 3.1.2
21
+ package :install_ruby do
22
+ install_rbenv 'deploy'
23
+ install_ruby 'deploy', '3.1.2', '--with-jemalloc'
24
+ end
25
+
26
+ # install yarn
27
+ package :yarn do
28
+ add_command 'curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -'
29
+ add_command 'echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list',
30
+ verify: has_file?('/etc/apt/sources.list.d/yarn.list')
31
+ apt_update
32
+ apt_install %w(nodejs yarn)
33
+ end
34
+
35
+ # install the caddy webserver
36
+ package :install_caddy do
37
+ apt_install %w(debian-keyring debian-archive-keyring apt-transport-https)
38
+ add_command "curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg", verifier: has_file?('/etc/apt/sources.list.d/caddy-stable.list')
39
+ add_command "curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list", verifier: has_file?('/etc/apt/sources.list.d/caddy-stable.list')
40
+
41
+ apt_update
42
+ apt_install %w(caddy)
43
+ directory "/etc/caddy/sites", owner: 'caddy:caddy'
44
+ file "/etc/caddy/Caddyfile", content: "import /etc/caddy/sites/*.caddy", owner: 'caddy:caddy'
45
+ file "/etc/caddy/sites/global.caddy", content: "
46
+ {
47
+ debug
48
+ log {
49
+ output file /var/log/caddy/caddy.log {
50
+ roll_size 10MB
51
+ }
52
+ }
53
+ }
54
+ ", owner: 'caddy:caddy'
55
+ chown '/etc/caddy/*', 'caddy:caddy'
56
+ add_command 'sudo adduser caddy deploy'
57
+ add_command 'sudo service caddy reload'
58
+ end
59
+
60
+ # create a static site
61
+ package :my_blog_com do
62
+ directory '/home/deploy/domains/myblog.com/public'
63
+ chown '/home/deploy/domains/myblog.com', 'deploy:deploy', "-R"
64
+ directory '/home/deploy/domains/myblog.com/shared/log', owner: "caddy:caddy"
65
+ file '/etc/caddy/sites/myblog.com.caddy', template: 'myblog.com.caddy'
66
+ chown '/etc/caddy/sites', 'caddy:caddy', "-R"
67
+ add_command "sudo service caddy reload"
68
+ end
69
+
70
+ # create a vhost for a Rails site
71
+ package :myrails_site_com do
72
+ directory '/home/deploy/domains/myrails-site.com/'
73
+ chown '/home/deploy/domains/', 'deploy:deploy'
74
+ file '/etc/caddy/sites/myrails-site.com.caddy', template: 'myrails-site.com.caddy'
75
+ chown '/etc/caddy/sites/*', 'caddy:caddy'
76
+ add_command "sudo service caddy reload"
77
+ end
78
+
79
+ # create a systemd service for puma
80
+ package :puma_myrails_site do
81
+ directory "/home/deploy/.config/systemd/user/", user: 'deploy'
82
+ chown "/home/deploy/.config", "deploy:deploy", "-R"
83
+ file "/home/deploy/.config/systemd/user/puma_myrails-site.service", owner: 'deploy:deploy', template: 'puma_myrails-site.service'
84
+
85
+ directory '/home/deploy/.config/systemd/user/default.target.wants', user: 'deploy'
86
+ symlink "/home/deploy/.config/systemd/user/default.target.wants/puma_myrails-site.service", "/home/deploy/.config/systemd/user/puma_myrails-site.service", user: 'deploy'
87
+
88
+ add_command "sudo -u deploy -l systemctl --user daemon-reload"
89
+ add_command "sudo -u deploy -l systemctl --user enable puma_myrails-site"
90
+ add_command "sudo loginctl enable-linger deploy"
91
+ end
@@ -0,0 +1,23 @@
1
+ www.myblog.com {
2
+ redir https://{host}{uri}
3
+ }
4
+
5
+ myblog.com {
6
+ root * /home/deploy/domains/myblog.com/public
7
+
8
+
9
+ log {
10
+ output file /home/deploy/domains/myblog.com/shared/log/access.log {
11
+ roll_size 10MB
12
+ roll_keep 10
13
+ }
14
+ }
15
+
16
+ encode zstd gzip
17
+
18
+ file_server
19
+
20
+ @notStatic {
21
+ not file
22
+ }
23
+ }
@@ -0,0 +1,25 @@
1
+ www.myrails-site.com {
2
+ redir https://{host}{uri}
3
+ }
4
+
5
+ myrails-site.com {
6
+ root * /home/deploy/domains/myrails-site.com/current/public
7
+
8
+
9
+ log {
10
+ output file /home/deploy/domains/myrails-site.com/shared/log/access.log {
11
+ roll_size 10MB
12
+ roll_keep 10
13
+ }
14
+ }
15
+
16
+ encode zstd gzip
17
+
18
+ file_server
19
+
20
+ @notStatic {
21
+ not file
22
+ }
23
+
24
+ reverse_proxy @notStatic unix//home/deploy/domains/myrails-site.com/shared/tmp/sockets/puma.sock
25
+ }
@@ -0,0 +1,21 @@
1
+ [Unit]
2
+ Description=Puma HTTP Server for myrails-site.com (production)
3
+ After=network.target
4
+
5
+ [Service]
6
+ Type=simple
7
+ WorkingDirectory=/home/deploy/domains/myrails-site.com/current
8
+ # Support older bundler versions where file descriptors weren't kept
9
+ # See https://github.com/rubygems/rubygems/issues/3254
10
+ ExecStart=/home/deploy/.rbenv/bin/rbenv exec bundle exec --keep-file-descriptors puma -C /home/deploy/domains/myrails-site.com/shared/puma.rb
11
+ ExecReload=/bin/kill -USR1 $MAINPID
12
+ StandardOutput=append:/home/deploy/domains/myrails-site.com/shared/log/puma_access.log
13
+ StandardError=append:/home/deploy/domains/myrails-site.com/shared/log/puma_error.log
14
+
15
+ Restart=always
16
+ RestartSec=1
17
+
18
+ SyslogIdentifier=puma
19
+
20
+ [Install]
21
+ WantedBy=multi-user.target
@@ -0,0 +1,3 @@
1
+ %deploy ALL= NOPASSWD: /bin/systemctl start puma_move_to_azores
2
+ %deploy ALL= NOPASSWD: /bin/systemctl stop puma_move_to_azores
3
+ %deploy ALL= NOPASSWD: /bin/systemctl restart puma_move_to_azores
@@ -0,0 +1,23 @@
1
+ require 'tty/option'
2
+ module Prepper
3
+ class Cli
4
+ include TTY::Option
5
+ usage do
6
+ program 'Prepper'
7
+ command 'run'
8
+ desc 'provision your server'
9
+ end
10
+
11
+ argument :config_file do
12
+ desc 'path to config file'
13
+ end
14
+
15
+ def run
16
+ if params[:help]
17
+ print help and exit
18
+ else
19
+ Prepper::Runner.run(File.read(params[:config_file]))
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ module Prepper
2
+ class Command
3
+ attr_reader :command, :user, :within, :env, :sudo, :opts, :verifier
4
+
5
+ def initialize(command, opts = {})
6
+ @command = command
7
+ @opts = opts
8
+ @user = opts[:user] || "root"
9
+ @within = opts[:within] || "/"
10
+ @env = opts[:env] || {}
11
+ @sudo = opts[:sudo] || false
12
+ @verifier = opts[:verifier]
13
+ end
14
+
15
+ def to_s
16
+ if @sudo
17
+ @command.dup.prepend("sudo ")
18
+ else
19
+ @command
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,85 @@
1
+ module Prepper
2
+ class Package
3
+ include SSHKit::DSL
4
+ include Tools::Apt
5
+ include Tools::Users
6
+ include Tools::File
7
+ include Tools::Text
8
+ include Tools::Rbenv
9
+
10
+ attr_accessor :name, :runner, :commands, :verifications
11
+
12
+ def initialize(name, opts = {}, &block)
13
+ @name = name
14
+ @opts = opts
15
+ @runner = opts[:runner]
16
+ @verifications = []
17
+ @commands = []
18
+ instance_eval &block if block_given?
19
+ end
20
+
21
+ def should_run?
22
+ return true if @verifications.empty?
23
+ return @verifications.all? do |verification|
24
+ !test_command(verification.call)
25
+ end
26
+ end
27
+
28
+ def verify(&block)
29
+ @verifications << block
30
+ end
31
+
32
+ def process
33
+ unless should_run?
34
+ SSHKit.config.output.write(SSHKit::LogMessage.new(1, "Skipping package #{name}"))
35
+ return
36
+ end
37
+ @commands.each do |command|
38
+ if command.verifier
39
+ if !test_command(command.verifier)
40
+ execute_command(command)
41
+ else
42
+ SSHKit.config.output.write(SSHKit::LogMessage.new(1, "Skipping command #{command.to_s}"))
43
+ end
44
+ else
45
+ execute_command(command)
46
+ end
47
+ end
48
+ end
49
+
50
+ def add_command(command, opts = {})
51
+ opts[:user] ||= "root"
52
+ opts[:within] ||= "/"
53
+ @commands << Command.new(command, opts)
54
+ end
55
+
56
+ def execute_command(command)
57
+ run_command(:execute, command)
58
+ end
59
+
60
+ def test_command(command)
61
+ run_command(:test, command)
62
+ end
63
+
64
+ def run_command(method, command)
65
+ on [runner.server_hash], in: :sequence do |host|
66
+ within command.within do
67
+ as command.user do
68
+ with command.env do
69
+ if respond_to? command.to_s.to_sym
70
+ send command.to_s.to_sym, *command.opts[:params]
71
+ else
72
+ if method == :execute
73
+ execute command.to_s
74
+ else
75
+
76
+ send(method, command.to_s)
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,59 @@
1
+ require 'sshkit'
2
+ require 'sshkit/dsl'
3
+ module Prepper
4
+ class Runner
5
+
6
+ attr_accessor :host, :packages, :commands, :user, :port
7
+
8
+ def self.run(config)
9
+ runner = new
10
+ runner.instance_eval config
11
+ runner.run
12
+ end
13
+
14
+ def initialize
15
+ @packages = []
16
+ @commands = []
17
+ @user = "root"
18
+ @port = 22
19
+ end
20
+
21
+ def run
22
+ puts "running on #{host}"
23
+ @packages.each(&:process)
24
+ end
25
+
26
+ def server_host(host)
27
+ @host = host
28
+ end
29
+
30
+ def server_user(user)
31
+ @user = user
32
+ end
33
+
34
+ def server_port(port)
35
+ @port = port
36
+ end
37
+
38
+ def ssh_options(ssh_options)
39
+ @ssh_options = ssh_options
40
+ end
41
+
42
+ def server_hash
43
+ {hostname: host, user: user, port: port, ssh_options: @ssh_options}
44
+ end
45
+
46
+ def add_command(command, opts = {})
47
+ package = Package.new("base", opts)
48
+ package.runner = self
49
+ opts[:user] ||= "root"
50
+ opts[:within] ||= "/"
51
+ package.commands << Command.new(command, opts)
52
+ @packages << package
53
+ end
54
+
55
+ def package(name, opts = {}, &block)
56
+ @packages << Package.new(name, opts.merge(runner: self), &block)
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,27 @@
1
+ module Prepper
2
+ module Tools
3
+ module Apt
4
+ def self.included(base)
5
+ base.class_eval do
6
+
7
+ # Updates apt repositories
8
+ def apt_update
9
+ @commands << Command.new("apt update", sudo: true)
10
+ end
11
+
12
+ # Installs packages
13
+ # @param packages [Array] array of package names
14
+ def apt_install(packages)
15
+ packages.each do |package|
16
+ @commands << Command.new("apt install --force-yes -qyu #{package}", sudo: true, verify: has_apt_package?(package))
17
+ end
18
+ end
19
+
20
+ def has_apt_package?(package)
21
+ Command.new("dpkg --status #{package} | grep 'ok installed'", sudo: true)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,68 @@
1
+ require 'erb'
2
+ require 'digest/md5'
3
+ module Prepper
4
+ module Tools
5
+ module File
6
+ def self.included(base)
7
+ base.class_eval do
8
+
9
+ def chown(path, owner, flags = "")
10
+ @commands << Command.new("chown #{flags} #{owner} #{path}", sudo: true)
11
+ end
12
+
13
+ def directory(path, opts = {})
14
+ @commands << Command.new("mkdir -p #{path}", opts.merge(sudo: true, verifier: has_directory?(path)))
15
+ @commands << Command.new("chown #{opts[:owner]} #{path}", sudo: true) if opts[:owner]
16
+ @commands << Command.new("chmod #{opts[:mode]} #{path}", sudo: true) if opts[:mode]
17
+ end
18
+
19
+ def has_directory?(path)
20
+ Command.new("test -d #{path}", sudo: true)
21
+ end
22
+
23
+ def file(path, opts = {})
24
+ opts[:locals] ||= {}
25
+ opts[:verify_content] ||= true
26
+ content = opts[:content] || render_template(opts[:template], opts[:locals])
27
+ verifier = if opts[:verify_content]
28
+ matches_content?(path, content)
29
+ else
30
+ has_file?(path)
31
+ end
32
+ io = StringIO.new(content)
33
+ @commands << Command.new("put!", {params: [io, path, {owner: opts[:owner], mode: opts[:mode]}], verifier: verifier})
34
+ end
35
+
36
+ def has_file?(path)
37
+ Command.new("test -f #{path}", sudo: true)
38
+ end
39
+
40
+ def matches_content?(path, content)
41
+ md5 = Digest::MD5.hexdigest(content)
42
+ Command.new("md5sum #{path} | cut -f1 -d' '`\" = \"#{md5}\"", sudo: true, verifier: has_file?(path))
43
+ end
44
+
45
+ def symlink(link, target, opts = {})
46
+ opts.merge!(
47
+ sudo: true,
48
+ verifier: has_symlink?(link)
49
+ )
50
+ @commands << Command.new("ln -s #{target} #{link}", opts)
51
+ end
52
+
53
+ def has_symlink?(link, file = nil)
54
+ if file
55
+ Command.new("'#{file}' = `readlink #{link}`")
56
+ else
57
+ Command.new("test -L #{link}", sudo: true)
58
+ end
59
+ end
60
+
61
+ def render_template(template, locals)
62
+ ERB.new(::File.read("./templates/#{template}")).result_with_hash(locals)
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,28 @@
1
+ module Prepper
2
+ module Tools
3
+ module Rbenv
4
+ def self.included(base)
5
+ base.class_eval do
6
+
7
+ def install_rbenv(user)
8
+ apt_install %w{libssl-dev zlib1g zlib1g-dev libreadline-dev}
9
+ @commands << Command.new("sudo -u #{user} -i git clone https://github.com/sstephenson/rbenv.git /home/#{user}/.rbenv", verifier: has_directory?("/home/#{user}/.rbenv"))
10
+ @commands << Command.new("sudo -u #{user} -i git clone https://github.com/sstephenson/ruby-build.git /home/#{user}/.rbenv/plugins/ruby-build", verifier: has_directory?("/home/#{user}/.rbenv/plugins/ruby-build"))
11
+
12
+ append_text 'export PATH="$HOME/.rbenv/bin:$PATH"', "/home/#{user}/.profile"
13
+ append_text 'eval "$(rbenv init -)"', "/home/#{user}/.profile"
14
+ chown "/home/#{user}/.profile", 'deploy:deploy'
15
+ end
16
+
17
+ def install_ruby(user, version, opts = '')
18
+ @commands << Command.new("sudo -u #{user} -i RUBY_CONFIGURE_OPTS='#{opts}' rbenv install #{version}", verifier: has_directory?("/home/#{user}/.rbenv/versions/#{version}"))
19
+
20
+ @commands << Command.new("sudo -u #{user} -i rbenv rehash")
21
+ @commands << Command.new("sudo -u #{user} -i rbenv global #{version}")
22
+ @commands << Command.new("sudo -u #{user} -i rbenv rehash")
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,19 @@
1
+ module Prepper
2
+ module Tools
3
+ module Text
4
+ def self.included(base)
5
+ base.class_eval do
6
+
7
+ def append_text(text, path)
8
+ @commands << Command.new("/bin/echo -e '#{text}' | sudo tee -a #{path}", verifier: has_text?(text, path))
9
+ end
10
+
11
+ def has_text?(text, path)
12
+ regex = Regexp.escape(text)
13
+ Command.new("grep -qPzo '^#{regex}$' #{path} ||", sudo: true)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,23 @@
1
+ module Prepper
2
+ module Tools
3
+ module Users
4
+ def self.included(base)
5
+ base.class_eval do
6
+ def add_user(username, opts = {})
7
+ opts[:flags] << ' --gecos ,,,'
8
+ @commands << Command.new("adduser #{username} #{opts[:flags]}", sudo: true, verifier: has_user?(username))
9
+ end
10
+
11
+ def has_user?(username, opts = {})
12
+ if opts[:in_group]
13
+ command = "id -nG #{username} | xargs -n1 echo | grep #{opts[:in_group]}"
14
+ else
15
+ command = "id #{username}"
16
+ end
17
+ Command.new(command, sudo: true)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module Prepper
2
+ VERSION = "0.1.0"
3
+ end
data/lib/prepper.rb ADDED
@@ -0,0 +1,12 @@
1
+ require "zeitwerk"
2
+ loader = Zeitwerk::Loader.for_gem
3
+ loader.ignore("#{__dir__}/sshkit_ext.rb")
4
+ loader.setup
5
+
6
+ require 'sshkit_ext'
7
+ require 'shellwords'
8
+
9
+ module Prepper
10
+ class Error < StandardError; end
11
+ # Your code goes here...
12
+ end
data/lib/sshkit_ext.rb ADDED
@@ -0,0 +1,33 @@
1
+ require 'sshkit'
2
+ SSHKit::Backend::Netssh.class_eval do
3
+ # Uploads the given string or file-like object to the current host
4
+ # context. Accepts :owner and :mode options that affect the permissions of the
5
+ # remote file.
6
+ #
7
+ def put!(string_or_io, remote_path, opts={})
8
+ sudo_exec = ->(*cmd) {
9
+ cmd = [:sudo] + cmd if opts[:sudo]
10
+ execute *cmd
11
+ }
12
+
13
+ tmp_path = "/tmp/#{SecureRandom.uuid}"
14
+
15
+ owner = opts[:owner]
16
+ mode = opts[:mode]
17
+
18
+ source = if string_or_io.respond_to?(:read)
19
+ string_or_io
20
+ else
21
+ StringIO.new(string_or_io.to_s)
22
+ end
23
+
24
+ sudo_exec.call :mkdir, "-p", File.dirname(remote_path)
25
+
26
+ upload!(source, tmp_path)
27
+
28
+ sudo_exec.call(:mv, "-f", tmp_path, remote_path)
29
+ sudo_exec.call(:chown, owner, remote_path) if owner
30
+ sudo_exec.call(:chmod, mode, remote_path) if mode
31
+ end
32
+
33
+ end
data/prepper.gemspec ADDED
@@ -0,0 +1,34 @@
1
+ require_relative 'lib/prepper/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "prepper"
5
+ spec.version = Prepper::VERSION
6
+ spec.authors = ["Greg Molnar"]
7
+ spec.email = ["molnargerg@gmail.com"]
8
+
9
+ spec.summary = "Simple server provisioning"
10
+ spec.description = "Simple server provisioning "
11
+ spec.homepage = "https://github.com/gregmolnar/prepper"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/gregmolnar/prepper"
19
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "bin"
27
+ spec.executables = "prepper"
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_dependency 'zeitwerk'
31
+ spec.add_dependency 'sshkit'
32
+ spec.add_dependency 'tty-option'
33
+ spec.add_development_dependency 'byebug'
34
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prepper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Greg Molnar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-12-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: zeitwerk
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sshkit
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: tty-option
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: 'Simple server provisioning '
70
+ email:
71
+ - molnargerg@gmail.com
72
+ executables:
73
+ - prepper
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/prepper
87
+ - bin/setup
88
+ - examples/config.rb
89
+ - examples/templates/myblog.caddy
90
+ - examples/templates/myrails-site.com.caddy
91
+ - examples/templates/myrails-site.service
92
+ - examples/templates/sudoers
93
+ - lib/prepper.rb
94
+ - lib/prepper/cli.rb
95
+ - lib/prepper/command.rb
96
+ - lib/prepper/package.rb
97
+ - lib/prepper/runner.rb
98
+ - lib/prepper/tools/apt.rb
99
+ - lib/prepper/tools/file.rb
100
+ - lib/prepper/tools/rbenv.rb
101
+ - lib/prepper/tools/text.rb
102
+ - lib/prepper/tools/users.rb
103
+ - lib/prepper/version.rb
104
+ - lib/sshkit_ext.rb
105
+ - prepper.gemspec
106
+ homepage: https://github.com/gregmolnar/prepper
107
+ licenses:
108
+ - MIT
109
+ metadata:
110
+ homepage_uri: https://github.com/gregmolnar/prepper
111
+ source_code_uri: https://github.com/gregmolnar/prepper
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: 2.3.0
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubygems_version: 3.1.4
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Simple server provisioning
131
+ test_files: []