Skip to content
This repository has been archived by the owner on Jul 29, 2018. It is now read-only.

Config + hook approach for restarting docker #54

Merged
merged 1 commit into from
Mar 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## v0.0.3 Feb 25, 2016
- Fix #12 and #21: Restart docker service on 'vagrant up' @budhrg
- Fix #74: vagrant-service-manager plugin version 0.0.3 release @navidshaikh
- Fix #45: Adds exit status for commands and invalid commands @navidshaikh
- Enhanced the developer instructions for developing the plugin in README @budhrg
Expand Down
35 changes: 20 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
# vagrant-service-manager

Provides setup information, including environment
variables and certificates, required to access
services provided by an [Atomic Developer Bundle
(ADB)](https://github.com/projectatomic/adb-atomic-developer-bundle).
This plugin makes it easier to use the ADB with host-based tools
such as Eclipse and the docker and kubernetes CLI commands.
Details on this usage pattern can be found in the [ADB
Documentation](https://github.com/projectatomic/adb-atomic-developer-bundle/blob/master/docs/using.rst).
This plugin provides setup information, including environment variables and certificates, required to access services provided by an [Atomic Developer Bundle (ADB)](https://github.com/projectatomic/adb-atomic-developer-bundle). This plugin makes it easier to use the ADB with host-based tools such as Eclipse and the docker and kubernetes CLI commands. Details on this usage pattern can be found in the [ADB Documentation](https://github.com/projectatomic/adb-atomic-developer-bundle/blob/master/docs/using.rst).

##Objective

Expand All @@ -27,16 +20,28 @@ producing complex, multi-container applications.

## Quick Start

1. Install `vagrant-service-manager` plugin
1. Install `vagrant-service-manager` plugin:

vagrant plugin install vagrant-service-manager

2. Get the [Vagrantfile](Vagrantfile)
and start the ADB using `vagrant up`.
[More](https://github.com/projectatomic/adb-atomic-developer-bundle/blob/master/docs/installing.rst)
documentation on setting up ADB.
2. Get a Vagrantfile for your box. Users of the
[Atomic Developer Bundle (ADB)](https://github.com/projectatomic/adb-atomic-developer-bundle) should download a [Vagrantfile from the repository](https://github.com/projectatomic/adb-atomic-developer-bundle/tree/master/components).

3. Run the plugin to get environment variables and certificates
3. Enable your desired service(s) in [Vagrantfile](Vagrantfile) as:

config.servicemanager.services = 'openshift'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be "service-manager" to match the plugin name, imho.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bexelbie : Here, it is not accepting - based name.


*Note*

* `docker` is default service and does not require above configuration.
* Enable multiple services as comma separated list. Eg: 'docker, openshift'

5. Start the ADB using `vagrant up`. Users of the ADB may wish to consult the
[Installation Documentation](https://github.com/projectatomic/adb-atomic-developer-bundle/blob/master/docs/installing.rst).

6. Run the plugin to get environment variables and certificates:

$ vagrant service-manager env docker

# Copying TLS certificates to /home/nshaikh/vagrant/adb1.7/.vagrant/machines/default/virtualbox/docker
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's update with newer output that shows the full command. Perhaps we should do a vagrant service-manager env on a box with openshift running.

# Set the following environment variables to enable access to the
Expand All @@ -48,7 +53,7 @@ documentation on setting up ADB.
# run following command to configure your shell:
# eval "$(vagrant service-manager env docker)"

4. Begin using your host-based tools.
7. Begin using your host-based tools.

## Exit codes

Expand Down
14 changes: 14 additions & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :

unless Vagrant.has_plugin?("vagrant-service-manager")
$stderr.puts <<-MSG
vagrant-service-manager plugin is required for projectatomic/adb.
Kindly install the plugin: `$ vagrant plugin install vagrant-service-manager`
MSG
exit 126
end

Vagrant.configure(2) do |config|

config.vm.box = "projectatomic/adb"

config.vm.network "private_network", type: "dhcp"

# This is the default setup
# config.servicemanager.services = 'docker'

# Enable multiple services as comma separated list.
# config.servicemanager.services = 'docker, openshift'
end
5 changes: 4 additions & 1 deletion lib/vagrant-service-manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
require 'vagrant-service-manager/os'

module Vagrant
module DockerInfo
module ServiceManager
# Returns the path to the source of this plugin
def self.source_root
@source_root ||= Pathname.new(File.expand_path('../../', __FILE__))
end

# Temporally load the extra capabilities files for Red Hat
load(File.join(source_root, 'plugins/guests/redhat/plugin.rb'))
end
end
7 changes: 0 additions & 7 deletions lib/vagrant-service-manager/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,6 @@ def execute_docker_info
# First, get the TLS Certificates, if needed
if !File.directory?(secrets_path) then

# Regenerate the certs and restart docker daemon in case of the new ADB box and for VirtualBox provider
if machine.provider_name == :virtualbox then
# `test` checks if the file exists, and then regenerates the certs and restart the docker daemon, else do nothing.
command2 = "test ! -f /opt/adb/cert-gen.sh || (sudo rm /etc/docker/ca.pem && sudo systemctl restart docker)"
machine.communicate.execute(command2)
end

# Get the private key
hprivate_key_path = machine.ssh_info[:private_key_path][0]

Expand Down
50 changes: 50 additions & 0 deletions lib/vagrant-service-manager/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'set'

module Vagrant
module ServiceManager
SERVICES = ['docker', 'openshift']

class Config < Vagrant.plugin('2', :config)
attr_accessor :services

DEFAULTS = {
services: 'docker'
}

def initialize
super
@services = UNSET_VALUE
end

def finalize!
DEFAULTS.each do |name, value|
if instance_variable_get('@' + name.to_s) == UNSET_VALUE
instance_variable_set '@' + name.to_s, value
end
end
end

def validate(machine)
errors = _detected_errors
errors.concat(validate_services)
{ "servicemanager" => errors }
end

private

def validate_services
errors = []

unless is_supported_services?
errors << "services should be subset of #{SERVICES.inspect}.}"
end

errors
end

def is_supported_services?
@services.split(',').map(&:strip).to_set.subset?(SERVICES.to_set)
end
end
end
end
12 changes: 12 additions & 0 deletions lib/vagrant-service-manager/plugin.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Loads all services
Dir["#{File.dirname(__FILE__)}/services/*.rb"].each { |f| require_relative f }

module Vagrant
module ServiceManager
class Plugin < Vagrant.plugin('2')
Expand All @@ -8,6 +11,15 @@ class Plugin < Vagrant.plugin('2')
require_relative 'command'
Command
end

config 'servicemanager' do
require_relative 'config'
Config
end

action_hook(:servicemanager, :machine_action_up) do |hook|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should only run when a config.service-manager line is in the Vagrantfile, otherwise it runs on every box I start whether it should or not. The only other way is to detect ADB and CDK which we can't do afaik right now.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After adding the guest detection capability, this is resolved. Now, it will only run against supported [ADB / CDK] boxes and docker is the default provider.

hook.append(Service::Docker)
end
end
end
end
29 changes: 29 additions & 0 deletions lib/vagrant-service-manager/services/docker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Vagrant
module ServiceManager
SUPPORTED_BOXES = ['adb', 'cdk']

module Service
class Docker
def initialize(app, env)
@app = app
@machine = env[:machine]
@ui = env[:ui]
end

def call(env)
if SUPPORTED_BOXES.include? @machine.guest.capability(:flavor)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this inside of the docker hook and not at a higher level? This structure means that every time we add a new service we have to write this same code again.

I am going to LGTM this, but I am opening an issue to get this fixed as we need to get openshift, etc. added.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed @bexelbie . Will work on it.

command = "sudo rm /etc/docker/ca.pem && sudo systemctl restart docker"
@machine.communicate.execute(command) do |type, data|
if type == :stderr
@ui.error(data)
exit 126
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good!

end
end
end

@app.call(env)
end
end
end
end
end
17 changes: 17 additions & 0 deletions plugins/guests/redhat/cap/flavor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module VagrantPlugins
module GuestRedHat
module Cap
class Flavor
def self.flavor(machine)
machine.communicate.sudo("grep VARIANT_ID /etc/os-release") do |type, data|
if type == :stderr
@env.ui.error(data)
exit 126
end
return data.chomp.gsub(/"/, '').split("=").last
end
end
end
end
end
end
12 changes: 12 additions & 0 deletions plugins/guests/redhat/plugin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'vagrant'

module VagrantPlugins
module GuestRedHat
class Plugin < Vagrant.plugin('2')
guest_capability('redhat', 'flavor') do
require_relative 'cap/flavor'
Cap::Flavor
end
end
end
end