diff --git a/Vagrantfile b/Vagrantfile index b9761f7..1ddbf53 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -2,8 +2,11 @@ # vi: set ft=ruby : Vagrant.configure(2) do |config| + config.vm.box = "projectatomic/adb" config.vm.network "private_network", type: "dhcp" + config.servicemanager.providers = 'docker' + end diff --git a/lib/vagrant-service-manager/action/restart_service.rb b/lib/vagrant-service-manager/action/restart_service.rb new file mode 100644 index 0000000..3f393dd --- /dev/null +++ b/lib/vagrant-service-manager/action/restart_service.rb @@ -0,0 +1,34 @@ +module Vagrant + module ServiceManager + module Action + class RestartService + def initialize(app, env) + @app = app + @machine = env[:machine] + @ui = env[:ui] + @providers = @machine.config.servicemanager.providers.split(',').map(&:chomp) + end + + def execute_command(command, provider) + @machine.communicate.execute(command) do |type, data| + if type == :stderr + @ui.error(data) + else + @ui.info("# Restarted #{provider} service.") + end + end + end + + def call(env) + @providers.each do |provider| + command = "sudo systemctl restart #{provider}" + command = "sudo rm /etc/docker/ca.pem && " + command if provider == 'docker' + execute_command(command, provider) + end + + @app.call(env) + end + end + end + end +end diff --git a/lib/vagrant-service-manager/command.rb b/lib/vagrant-service-manager/command.rb index 8571b1c..1542b8d 100644 --- a/lib/vagrant-service-manager/command.rb +++ b/lib/vagrant-service-manager/command.rb @@ -104,13 +104,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] diff --git a/lib/vagrant-service-manager/config.rb b/lib/vagrant-service-manager/config.rb new file mode 100644 index 0000000..bccc4cc --- /dev/null +++ b/lib/vagrant-service-manager/config.rb @@ -0,0 +1,22 @@ +module Vagrant + module ServiceManager + class Config < Vagrant.plugin('2', :config) + attr_accessor :providers + + def initialize + super + @providers = UNSET_VALUE + end + + def finalize! + # enable docker as provider by default + @providers = 'docker' if @providers == UNSET_VALUE + end + + def validate(machine) + errors = _detected_errors + { "servicemanager" => errors } + end + end + end +end diff --git a/lib/vagrant-service-manager/plugin.rb b/lib/vagrant-service-manager/plugin.rb index f4affd2..212133f 100644 --- a/lib/vagrant-service-manager/plugin.rb +++ b/lib/vagrant-service-manager/plugin.rb @@ -1,3 +1,5 @@ +require_relative "action/restart_service" + module Vagrant module ServiceManager class Plugin < Vagrant.plugin('2') @@ -8,6 +10,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| + hook.append(Action::RestartService) + end end end end