Kanrisuru

Upload Config Templates

Kanrisuru has an upload and download command that makes it easy to transfer files to and from your remote machine.

With provisioning and maintaining a remote server, you can leverage the ERB templates that’s apart of the Kanrisuru library.

As an example, consider the following config file saved in the local project directory:

# ~/project/templates/nginx/site.conf.erb
upstream <%= @upstream_name %> {
  <% @upstream_cluster.each do |server| -%>
  server <%= server[:host] %>:<%= server[:port] %>;
  <% end -%>
}

server {
  listen *:<%= @port %>;
  server_name <%= @server_name %>;

  location / {
    proxy_cache_bypass $http_upgrade;
    proxy_http_version 1.1;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection Upgrade;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-Ssl on;
    proxy_set_header X-Forwarded-Proto $server_port;
    proxy_set_header X-Forwarded-Host $host;

    proxy_pass http://<%= @upstream_name %>;
  }
}


To upload the config file in your local project directory to a remote machine:

require 'kanrisuru'
host = Kanrisuru::Remote::Host.new(host: 'ubuntu-host', username: 'ubuntu', port: 22, keys: ['~/.ssh/id_rsa'])

domain = "www.example.com"
path = "templates/nginx/site.conf.erb"

template = Kanrisuru::Template.new(path,
  port: 80, 
  server_name: domain, 
  upstream_name: 'app',
  upstream_cluster: [{
    host: 'app0.prod.xyz',
    port: 3000
  }, {
    host: 'app1.prod.xyz',
    port: 3000
  }, {
    host: 'app2.prod.xyz',
    port: 3000
  }, {
    host: 'app3.prod.xyz',
    port: 3000
  }, {
    host: 'app4.prod.xyz',
    port: 3000
  }, {
    host: 'app5.prod.xyz',
    port: 3000 
  }]
)

result = host.upload(template.read, "/etc/nginx/sites-available/#{domain}.conf")
result.success?
true

result.path
"/etc/nginx/sites-available/www.example.com.conf"


You can verify that the config file was uploaded properly:

host.cat('/etc/nginx/sites-available/www.example.com.conf').command.raw_result
upstream app {
    server app0.prod.xyz:3000;
    server app1.prod.xyz:3000;
    server app2.prod.xyz:3000;
    server app3.prod.xyz:3000;
    server app4.prod.xyz:3000;
    server app5.prod.xyz:3000;
}

server {
  listen *:80;
  server_name www.example.com;

  location / {
    proxy_cache_bypass $http_upgrade;
    proxy_http_version 1.1;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection Upgrade;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-Ssl on;
    proxy_set_header X-Forwarded-Proto $server_port;
    proxy_set_header X-Forwarded-Host $host;

    proxy_pass http://app;
  }
}
On this page