Last active
November 11, 2017 17:47
-
-
Save gionn/f473a77998d8edb33c45 to your computer and use it in GitHub Desktop.
Fabric script to manage remote chef-client local_mode nodes. (Deprecated the old chef-solo, it support search API without any additional plugin)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fabric.api import * | |
from fabric.colors import * | |
from fabric.contrib import console | |
from fabric.operations import prompt | |
import json | |
import os | |
env.colorize_errors = True | |
env.use_ssh_config = True | |
env.ssh_config_path = './ssh-config' | |
try: | |
env.chef_version | |
except AttributeError: | |
env.chef_version = 'latest' | |
try: | |
env.chef_opts | |
except AttributeError: | |
env.chef_opts = '' | |
@hosts('staging') | |
def update_staging(): | |
env.chef_environment = 'dev' | |
provision() | |
def bootstrap(): | |
install_chef() | |
provision() | |
def install_chef(): | |
output = run("apt-cache show chef 2>&1 | grep 'Version: %s' | wc -l" % | |
(env.chef_version)) | |
if int(output) == 0: | |
sudo('apt-get install curl -qy') | |
sudo('curl -L https://www.chef.io/chef/install.sh | bash -s -- -v %s' % | |
(env.chef_version)) | |
else: | |
print green('chef %s already installed' % (env.chef_version)) | |
def provision(): | |
install_chef() | |
sync_cookbooks() | |
run_chef() | |
def sync_cookbooks(): | |
tmp_folder = '/tmp/berkshelf_{}'.format(env.host_string) | |
run('mkdir -p /tmp/chef') | |
local('rm -fr {0} && berks vendor {0}'.format(tmp_folder)) | |
local('rsync -az --delete -e "ssh -F ssh-config" --exclude .git {}/ {}:/tmp/chef/cookbooks/'.format(tmp_folder, env.host_string)) | |
local('rsync -az --delete -e "ssh -F ssh-config" --exclude .git ./data_bags/ {}:/tmp/chef/data_bags/'.format(env.host_string)) | |
local('rsync -az --delete -e "ssh -F ssh-config" --exclude .git ./environments/ {}:/tmp/chef/environments/'.format(env.host_string)) | |
put('nodes/{}.json'.format(env.host_string), 'dna.json') | |
put('config/client.rb', 'client.rb') | |
def run_chef(): | |
sudo('chef-client -z -E {} -c client.rb -j dna.json {}'.format(env.chef_environment, env.chef_opts) ) | |
def clean(): | |
sudo('rm -fr /tmp/chef/cookbooks /tmp/chef/environments /tmp/chef/data_bags') | |
def push_sources_list(): | |
put('config/sources.list', '/etc/apt/sources.list') | |
sudo('apt-get update -q') | |
def run_apt_upgrade(): | |
sudo('apt-get update -q') | |
sudo('apt-get dist-upgrade -q -y') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment