1. update travis deploy

2. update README to adapt change
3. use IsFlagged class for flag option
4. add destroy method
This commit is contained in:
Freddy Tan 2016-11-19 18:24:33 +08:00
parent 201082fefb
commit bd1d08af71
5 changed files with 167 additions and 30 deletions

View file

@ -22,4 +22,27 @@ install:
script: script:
- "export PATH=$PATH:$PWD/tf_bin" - "export PATH=$PATH:$PWD/tf_bin"
- cd test - cd test
- py.test - py.test
deploy:
# test pypi
- provider: pypi
distributions: sdist
server: https://testpypi.python.org/pypi
user: "beelit94"
password:
secure: "sWxc+p/gdq3k2WbUGNG2F4TukFNkTkvq6OPaiOvyfgWThYNk6/juRkMd8flmTbh0VGhcjFbpDLeSApb2kFhfiokYJSH1hOOcmXf8xzYH8/+R4DDEiGa5Y/pR9TBvYu4S8eJEfFUFfb1BBpapykj7o43hcaqMExBJIdVJU7aeoEAC1jQeTJh8wWwdJKHy2dNSM+6RVhk3e5+b0LfK7Bk5sU5P+YdEMj79MJU450J4OmZXWzJgvBN5/2QfVa5LrUD00nYuGuiBniz2lVevIHWjUYawUzpPsTa7F0s2WemG9YcV7U8u06xNjY9Ce3CTbxNhc7OIKq+TCkOgR3qZFXVJ8A87G+AT2iQ01VslQ4DJCxnJNTnpqojWnwf6MFL9O8ONioWYO32bhQFKOQ806ASHP4lNMRDKqx8hXtP5In7/r0SARKscv6Bas83rp+FESkKD5vWgkZJG+yx96LlwRLUhSVnVyb/nOJ++zt5RR3BvY2O4p9YAZY3Qt8TQihOdBQKnY3UXsMyNaE25+yvyNWpmyJiePRbTUd+cpLnycnqG9Ll8v6TpFXb6ahFMjlAFfJNQYlREfseClTHSRjZNxfsXGQCsJh6TZAq7jOB5hCk3q41eOUFWARxbyj8j59NBV8fSQrrGJJ9/VZKQeYiQlBB9KpK4PrnH84oeQ8i+VSbVr5w="
on:
branch: release
tags: false
condition: $TRAVIS_PYTHON_VERSION = "2.7"
# production pypi
- provider: pypi
distributions: sdist
user: "beelit94"
password:
secure: "QhCiTLrBvw/Uzt3eiLEmvMP3uHnayVCETqEDA+2+Q9vFavqj0CHA76zqYonBFqnh0a3HFCRIVVt+6ynpZ10kpQ3tAObIw+pY39ZPnpAhOjSpFzzMdpIF9Bhv9A93ng2iSESAZPAOwktHzUwjFx0Zvl0lSYD9rutHgttGgdU2CajiUtwTUhCTjOAVdR2Gm+15H808vzKWnMaKflXxZt+fkt279mQTYAtz6eBWtZwIKry/uAJCSrPSWtbi50O0HsWRMXLXWH5Jn/BVjWSDSM92DssUDq0D+tQyp4M5nQXJ9EyAvEdsKNLx3cvNruznh2ohI2jmcoIjwFiS6+wrEmUiXkP86iyzCSqL/EbcOG0xUh3vbfYtMBp7jENgD405+3SEhPY4PlqUmc+HDtB7FUcHz4y7wGWJRGyQzNnjJ6Tv0Ajdz5mfJubWVIvHjcRqkxTVtUKt50o00xZ62M0ZzQkDTIHQEsZly0XeHAgSvNzWkmjt9BiBrZ9OkoWVkRpSrCBy/EcpDNPCTSfSzOQ0Nq1ePFjkkW1n8QWDW9Pdb+/7/P2y9E2S8CT+nXBkRQeQiO86Qf1Ireg7k9TA5VYisVZ6bEXEc9UV0mAojpSsC7zWhVlbAoltN6ZbjKmqy/wqn2QIcJemcSie0JigzKpdw7l8FPT2lCRyTKlYLpRyKXzSkNI="
on:
branch: master
tags: true
condition: $TRAVIS_PYTHON_VERSION = "2.7"

7
LICENSE.txt Normal file
View file

@ -0,0 +1,7 @@
Copyright (c) 2016 beelit94@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View file

@ -8,3 +8,57 @@ python-terraform is a python module provide a wrapper of `terraform` command lin
## Installation ## Installation
pip install git+https://github.com/beelit94/python-terraform.git@develop pip install git+https://github.com/beelit94/python-terraform.git@develop
## Usage
For any terraform command
def cmd(self, cmd, *args, **kwargs):
"""
run a terraform command, if success, will try to read state file
:param cmd: command and sub-command of terraform, seperated with space
refer to https://www.terraform.io/docs/commands/index.html
:param args: arguments of a command
:param kwargs: any option flag with key value without prefixed dash character
if there's a dash in the option name, use under line instead of dash,
ex. -no-color --> no_color
if it's a simple flag with no value, value should be IsFlagged
ex. cmd('taint', allow_missing=IsFlagged)
if it's a boolean value flag, assign True or false
if it's a flag could be used multiple times, assign list to it's value
if it's a "var" variable flag, assign dictionary to it
if a value is None, will skip this option
:return: ret_code, out, err
For apply/destroy method, the flag options, like, `-no-color` or `-force`
have been implemented as boolean argument. simply use `is_no_color=True/False` for
apply/destroy method
## Examples
### Have a test.tf file under folder "/home/test"
#### apply with variables a=b, c=d, refresh=False, no color in the output
In shell:
cd /home/test
terraform apply -var='a=b' -var='c=d' -refresh=false -no-color
In python-terraform:
from python_terraform import Terraform
tf = terraform(working_dir='/home/test')
tf.apply(is_no_color=True, refresh=False, var={'a':'b', 'c':'d'})
#### taint command, allow-missing and no color
In shell:
cd /home/test
terraform taint -allow-missing -no-color
In python-terraform:
from python_terraform import Terraform
tf = terraform(working_dir='/home/test')
tf.cmd('taint', allow_missing=IsFlagged, no_color=IsFlagged)

View file

@ -8,6 +8,14 @@ from python_terraform.tfstate import Tfstate
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
class IsFlagged:
pass
class IsNotFlagged:
pass
class Terraform: class Terraform:
""" """
Wrapper of terraform command line tool Wrapper of terraform command line tool
@ -21,6 +29,15 @@ class Terraform:
parallelism=None, parallelism=None,
var_file=None, var_file=None,
terraform_bin_path=None): terraform_bin_path=None):
"""
:param working_dir: the folder of the working folder, if not given, will be where python
:param targets: list of target
:param state: path of state file relative to working folder
:param variables: variables for apply/destroy/plan command
:param parallelism: parallelism for apply/destroy command
:param var_file: if specified, variables will not be used
:param terraform_bin_path: binary path of terraform
"""
self.working_dir = working_dir self.working_dir = working_dir
self.state = state self.state = state
self.targets = [] if targets is None else targets self.targets = [] if targets is None else targets
@ -29,42 +46,62 @@ class Terraform:
self.terraform_bin_path = terraform_bin_path \ self.terraform_bin_path = terraform_bin_path \
if terraform_bin_path else 'terraform' if terraform_bin_path else 'terraform'
self.var_file = var_file self.var_file = var_file
self.input = False
# store the tfstate data # store the tfstate data
self.tfstate = dict() self.tfstate = dict()
def apply(self, def apply(self,
working_dir=None, dir=None,
no_color=True, is_no_color=True,
is_input=False,
**kwargs): **kwargs):
""" """
refer to https://terraform.io/docs/commands/apply.html refer to https://terraform.io/docs/commands/apply.html
:param working_dir: working folder :raise RuntimeError when return code is not zero
:param no_color: Disables output with coloring. :param is_no_color: if True, add flag -no-color
:param is_input: if True, add option -input=true
:param dir: folder relative to working folder
:param kwargs: same as kwags in method 'cmd'
:returns return_code, stdout, stderr :returns return_code, stdout, stderr
""" """
if not working_dir:
working_dir = self.working_dir
args, option_dict = self._create_cmd_args(is_input,
is_no_color,
dir,
kwargs)
return self.cmd('apply', *args, **option_dict)
def _create_cmd_args(self, is_input, is_no_color, dir, kwargs):
option_dict = dict() option_dict = dict()
option_dict['state'] = self.state option_dict['state'] = self.state
option_dict['target'] = self.targets option_dict['target'] = self.targets
option_dict['var'] = self.variables option_dict['var'] = self.variables
option_dict['var_file'] = self.var_file option_dict['var_file'] = self.var_file
option_dict['parallelism'] = self.parallelism option_dict['parallelism'] = self.parallelism
if no_color: if is_no_color:
option_dict['no_color'] = '' option_dict['no_color'] = IsFlagged
option_dict['input'] = self.input option_dict['input'] = is_input
option_dict.update(kwargs) option_dict.update(kwargs)
args = [dir] if dir else []
return args, option_dict
args = [working_dir] if working_dir else [] def destroy(self, working_dir=None, is_force=True,
is_no_color=True, is_input=False, **kwargs):
"""
refer to https://www.terraform.io/docs/commands/destroy.html
:raise RuntimeError when return code is not zero
:return: ret_code, stdout, stderr
"""
ret, out, err = self.cmd('apply', *args, **option_dict) args, option_dict = self._create_cmd_args(is_input,
is_no_color,
working_dir,
kwargs)
if is_force:
option_dict['force'] = IsFlagged
if ret != 0: return self.cmd('destroy', *args, **option_dict)
raise RuntimeError(err)
def generate_cmd_string(self, cmd, *args, **kwargs): def generate_cmd_string(self, cmd, *args, **kwargs):
""" """
@ -82,11 +119,11 @@ class Terraform:
--> generate_cmd_string call: --> generate_cmd_string call:
terraform apply -var='a=b' -var='c=d' -no-color the_folder terraform apply -var='a=b' -var='c=d' -no-color the_folder
--> python call: --> python call:
tf.generate_cmd_string('apply', the_folder, no_color='', var={'a':'b', 'c':'d'}) tf.generate_cmd_string('apply', the_folder, no_color=IsFlagged, var={'a':'b', 'c':'d'})
:param cmd: command and sub-command of terraform, seperated with space :param cmd: command and sub-command of terraform, seperated with space
refer to https://www.terraform.io/docs/commands/index.html refer to https://www.terraform.io/docs/commands/index.html
:param args: argument other than options of a command :param args: arguments of a command
:param kwargs: same as kwags in method 'cmd' :param kwargs: same as kwags in method 'cmd'
:return: string of valid terraform command :return: string of valid terraform command
""" """
@ -110,10 +147,13 @@ class Terraform:
continue continue
# simple flag, # simple flag,
if v == '': if v is IsFlagged:
cmds += ['-{k}'.format(k=k)] cmds += ['-{k}'.format(k=k)]
continue continue
if v is IsNotFlagged:
continue
if not v: if not v:
continue continue
@ -131,10 +171,12 @@ class Terraform:
run a terraform command, if success, will try to read state file run a terraform command, if success, will try to read state file
:param cmd: command and sub-command of terraform, seperated with space :param cmd: command and sub-command of terraform, seperated with space
refer to https://www.terraform.io/docs/commands/index.html refer to https://www.terraform.io/docs/commands/index.html
:param args: argument other than options of a command :param args: arguments of a command
:param kwargs: any option flag with key value other than variables, :param kwargs: any option flag with key value without prefixed dash character
if there's a dash in the option name, use under line instead of dash, ex -no-color --> no_color if there's a dash in the option name, use under line instead of dash,
if it's a simple flag with no value, value should be empty string ex. -no-color --> no_color
if it's a simple flag with no value, value should be IsFlagged
ex. cmd('taint', allow_missing=IsFlagged)
if it's a boolean value flag, assign True or false if it's a boolean value flag, assign True or false
if it's a flag could be used multiple times, assign list to it's value if it's a flag could be used multiple times, assign list to it's value
if it's a "var" variable flag, assign dictionary to it if it's a "var" variable flag, assign dictionary to it
@ -144,8 +186,11 @@ class Terraform:
cmd_string = self.generate_cmd_string(cmd, *args, **kwargs) cmd_string = self.generate_cmd_string(cmd, *args, **kwargs)
log.debug('command: {c}'.format(c=cmd_string)) log.debug('command: {c}'.format(c=cmd_string))
working_folder = self.working_dir if self.working_dir else None
p = subprocess.Popen(cmd_string, stdout=subprocess.PIPE, p = subprocess.Popen(cmd_string, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True) stderr=subprocess.PIPE, shell=True,
cwd=working_folder)
out, err = p.communicate() out, err = p.communicate()
ret_code = p.returncode ret_code = p.returncode
log.debug('output: {o}'.format(o=out)) log.debug('output: {o}'.format(o=out))
@ -162,8 +207,9 @@ class Terraform:
:param name: name of output :param name: name of output
:return: output value :return: output value
""" """
ret, out, err = self.cmd('output', name, json='') ret, out, err = self.cmd('output', name, json=IsFlagged)
log.debug('output raw string: {0}'.format(out))
if ret != 0: if ret != 0:
return None return None
out = out.lstrip() out = out.lstrip()

View file

@ -1,16 +1,16 @@
from python_terraform import Terraform from python_terraform import *
import pytest import pytest
import os import os
import logging import logging
import re import re
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.WARN)
STRING_CASES = [ STRING_CASES = [
[ [
lambda x: x.generate_cmd_string('apply', 'the_folder', lambda x: x.generate_cmd_string('apply', 'the_folder',
no_color='', no_color=IsFlagged,
var={'a': 'b', 'c': 'd'}), var={'a': 'b', 'c': 'd'}),
"terraform apply -var='a=b' -var='c=d' -no-color the_folder" "terraform apply -var='a=b' -var='c=d' -no-color the_folder"
], ],
@ -27,7 +27,7 @@ CMD_CASES = [
['method', 'expected_output'], ['method', 'expected_output'],
[ [
[ [
lambda x: x.cmd('plan', 'apply_tf', no_color='', var={'test_var': 'test'}) , lambda x: x.cmd('plan', 'apply_tf', no_color=IsFlagged, var={'test_var': 'test'}) ,
"doesn't need to do anything" "doesn't need to do anything"
] ]
] ]
@ -73,9 +73,16 @@ class TestTerraform:
def test_apply(self): def test_apply(self):
tf = Terraform(working_dir='apply_tf', variables={'test_var': 'test'}) tf = Terraform(working_dir='apply_tf', variables={'test_var': 'test'})
tf.apply(var={'test_var': 'test2'}) ret, out, err = tf.apply(var={'test_var': 'test2'})
assert ret == 0
def test_get_output(self): def test_get_output(self):
tf = Terraform(working_dir='apply_tf', variables={'test_var': 'test'}) tf = Terraform(working_dir='apply_tf', variables={'test_var': 'test'})
tf.apply() tf.apply()
assert tf.output('test_output') == 'test' assert tf.output('test_output') == 'test'
def test_destroy(self):
tf = Terraform(working_dir='apply_tf', variables={'test_var': 'test'})
ret, out, err = tf.destroy()
assert ret == 0
assert 'Destroy complete! Resources: 0 destroyed.' in out