From d313971e1a1e79bf9a76dbd537cb1bb5298af572 Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Mon, 11 Apr 2016 19:21:11 +0200 Subject: [PATCH] Create __pyvenv type and modify __package_pip accordingly. --- cdist/conf/type/__package_pip/explorer/su | 27 +++++++ cdist/conf/type/__package_pip/explorer/sudo | 27 +++++++ cdist/conf/type/__package_pip/gencode-remote | 42 +++++++++- cdist/conf/type/__package_pip/man.text | 8 ++ .../type/__package_pip/parameter/optional | 1 + cdist/conf/type/__pyvenv/explorer/group | 5 ++ cdist/conf/type/__pyvenv/explorer/owner | 5 ++ cdist/conf/type/__pyvenv/explorer/state | 9 +++ cdist/conf/type/__pyvenv/gencode-remote | 68 +++++++++++++++++ cdist/conf/type/__pyvenv/man.text | 76 +++++++++++++++++++ cdist/conf/type/__pyvenv/manifest | 46 +++++++++++ .../type/__pyvenv/parameter/default/group | 1 + .../conf/type/__pyvenv/parameter/default/mode | 1 + .../type/__pyvenv/parameter/default/owner | 1 + .../type/__pyvenv/parameter/default/python | 1 + .../type/__pyvenv/parameter/default/state | 1 + .../__pyvenv/parameter/default/venvparams | 1 + cdist/conf/type/__pyvenv/parameter/optional | 6 ++ 18 files changed, 324 insertions(+), 2 deletions(-) create mode 100644 cdist/conf/type/__package_pip/explorer/su create mode 100644 cdist/conf/type/__package_pip/explorer/sudo create mode 100755 cdist/conf/type/__pyvenv/explorer/group create mode 100755 cdist/conf/type/__pyvenv/explorer/owner create mode 100755 cdist/conf/type/__pyvenv/explorer/state create mode 100755 cdist/conf/type/__pyvenv/gencode-remote create mode 100755 cdist/conf/type/__pyvenv/man.text create mode 100755 cdist/conf/type/__pyvenv/manifest create mode 100755 cdist/conf/type/__pyvenv/parameter/default/group create mode 100755 cdist/conf/type/__pyvenv/parameter/default/mode create mode 100755 cdist/conf/type/__pyvenv/parameter/default/owner create mode 100644 cdist/conf/type/__pyvenv/parameter/default/python create mode 100755 cdist/conf/type/__pyvenv/parameter/default/state create mode 100644 cdist/conf/type/__pyvenv/parameter/default/venvparams create mode 100755 cdist/conf/type/__pyvenv/parameter/optional diff --git a/cdist/conf/type/__package_pip/explorer/su b/cdist/conf/type/__package_pip/explorer/su new file mode 100644 index 00000000..f355a1bc --- /dev/null +++ b/cdist/conf/type/__package_pip/explorer/su @@ -0,0 +1,27 @@ +#!/bin/sh +# +# 2016 Darko Poljak (darko.poljak at gmail.com) +# +# This file is part of cdist. +# +# cdist is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# cdist is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with cdist. If not, see . +# + +which su > /dev/null 2>&1 +if [ $? -eq 0 ] +then + echo yes +else + echo no +fi diff --git a/cdist/conf/type/__package_pip/explorer/sudo b/cdist/conf/type/__package_pip/explorer/sudo new file mode 100644 index 00000000..7b702bc0 --- /dev/null +++ b/cdist/conf/type/__package_pip/explorer/sudo @@ -0,0 +1,27 @@ +#!/bin/sh +# +# 2016 Darko Poljak (darko.poljak at gmail.com) +# +# This file is part of cdist. +# +# cdist is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# cdist is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with cdist. If not, see . +# + +which sudo > /dev/null 2>&1 +if [ $? -eq 0 ] +then + echo yes +else + echo no +fi diff --git a/cdist/conf/type/__package_pip/gencode-remote b/cdist/conf/type/__package_pip/gencode-remote index ba44927a..ccf30f1a 100644 --- a/cdist/conf/type/__package_pip/gencode-remote +++ b/cdist/conf/type/__package_pip/gencode-remote @@ -1,6 +1,7 @@ #!/bin/sh # # 2012 Nico Schottelius (nico-cdist at schottelius.org) +# 2016 Darko Poljak (darko.poljak at gmail.com) # # This file is part of cdist. # @@ -40,12 +41,49 @@ else pip="pip" fi +runasparam="$__object/parameter/runas" +if [ -f "$runasparam" ] +then + runas=$(cat "$runasparam") + has_sudo=$(cat "$__object/explorer/sudo") + if [ "$has_sudo" = "yes" ] + then + runas_cmd="sudo" + else + has_su=$(cat "$__object/explorer/su") + if [ "$has_su" = "yes" ] + then + runas_cmd="su" + else + runas_cmd="" + fi + fi +else + runas_cmd="" +fi + case "$state_should" in present) - echo $pip install -q "$name" + if [ "$runas_cmd" = "sudo" ] + then + echo sudo -H -u $runas $pip install -q "$name" + elif [ "$runas_cmd" = "su" ] + then + echo su $runas -c \"$pip install -q "$name"\" + else + echo $pip install -q "$name" + fi ;; absent) - echo $pip uninstall -q -y "$name" + if [ "$runas_cmd" = "sudo" ] + then + echo sudo -H -u $runas $pip uninstall -q -y "$name" + elif [ "$runas_cmd" = "su" ] + then + echo su $runas -c \"$pip uninstall -q -y "$name"\" + else + echo $pip uninstall -q -y "$name" + fi ;; *) echo "Unknown state: $state_should" >&2 diff --git a/cdist/conf/type/__package_pip/man.text b/cdist/conf/type/__package_pip/man.text index 5f619871..cb7c7e11 100644 --- a/cdist/conf/type/__package_pip/man.text +++ b/cdist/conf/type/__package_pip/man.text @@ -30,6 +30,11 @@ pip:: state:: Either "present" or "absent", defaults to "present" +runas:: + Run pip as specified user. By default it runs as root. + It uses sudo or su, whichever it founds first, respectively. + If no sudo nor su is present then pip is run by default. + EXAMPLES -------- @@ -40,6 +45,9 @@ __package_pip pyro --state present # Use pip in a virtualenv located at /root/shinken_virtualenv __package_pip pyro --state present --pip /root/shinken_virtualenv/bin/pip + +# Use pip in a virtualenv located at /foo/shinken_virtualenv as user foo +__package_pip pyro --state present --pip /foo/shinken_virtualenv/bin/pip --runas foo -------------------------------------------------------------------------------- diff --git a/cdist/conf/type/__package_pip/parameter/optional b/cdist/conf/type/__package_pip/parameter/optional index f32876f7..83265c8b 100644 --- a/cdist/conf/type/__package_pip/parameter/optional +++ b/cdist/conf/type/__package_pip/parameter/optional @@ -1,2 +1,3 @@ pip state +runas diff --git a/cdist/conf/type/__pyvenv/explorer/group b/cdist/conf/type/__pyvenv/explorer/group new file mode 100755 index 00000000..ff072c5e --- /dev/null +++ b/cdist/conf/type/__pyvenv/explorer/group @@ -0,0 +1,5 @@ +#!/bin/sh + +destination="/$__object_id" + +stat --print "%G" ${destination} 2>/dev/null || exit 0 diff --git a/cdist/conf/type/__pyvenv/explorer/owner b/cdist/conf/type/__pyvenv/explorer/owner new file mode 100755 index 00000000..b77e3c6e --- /dev/null +++ b/cdist/conf/type/__pyvenv/explorer/owner @@ -0,0 +1,5 @@ +#!/bin/sh + +destination="/$__object_id" + +stat --print "%U" ${destination} 2>/dev/null || exit 0 diff --git a/cdist/conf/type/__pyvenv/explorer/state b/cdist/conf/type/__pyvenv/explorer/state new file mode 100755 index 00000000..ffe3cbbd --- /dev/null +++ b/cdist/conf/type/__pyvenv/explorer/state @@ -0,0 +1,9 @@ +#!/bin/sh + +destination="/$__object_id" + +if [ -d "$destination" ]; then + echo present +else + echo absent +fi diff --git a/cdist/conf/type/__pyvenv/gencode-remote b/cdist/conf/type/__pyvenv/gencode-remote new file mode 100755 index 00000000..b0fa121d --- /dev/null +++ b/cdist/conf/type/__pyvenv/gencode-remote @@ -0,0 +1,68 @@ +#!/bin/sh +# +# 2016 Darko Poljak (darko.poljak at gmail.com) +# +# This file is part of cdist. +# +# cdist is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# cdist is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with cdist. If not, see . +# +# + +state_is="$(cat "$__object/explorer/state")" +owner_is="$(cat "$__object/explorer/owner")" +group_is="$(cat "$__object/explorer/group")" + +state_should="$(cat "$__object/parameter/state")" + +destination="/$__object_id" + +owner="$(cat "$__object/parameter/owner")" +group="$(cat "$__object/parameter/group")" +mode="$(cat "$__object/parameter/mode")" +python="$(cat "$__object/parameter/python")" +if [ "$python" ] +then + python_opt="-p $python" +else + python_opt="" +fi +venvparams="$(cat "$__object/parameter/venvparams")" + +[ "$state_should" = "$state_is" -a \ + "$owner" = "$owner_is" -a \ + "$group" = "$group_is" -a \ + -n "$mode" ] && exit 0 + +case $state_should in + present) + + if [ "$state_should" != "$state_is" ]; then + echo virtualenv "$python_opt" $venvparams "$destination" + fi + if [ \( -n "$owner" -a "$owner_is" != "$owner" \) -o \ + \( -n "$group" -a "$group_is" != "$group" \) ]; then + echo chown -R "${owner}:${group}" "$destination" + fi + if [ -n "$mode" ]; then + echo chmod -R "$mode" "$destination" + fi + ;; + absent) + ;; + + *) + echo "Unknown state: $state_should" >&2 + exit 1 + ;; +esac diff --git a/cdist/conf/type/__pyvenv/man.text b/cdist/conf/type/__pyvenv/man.text new file mode 100755 index 00000000..2f9a6ea5 --- /dev/null +++ b/cdist/conf/type/__pyvenv/man.text @@ -0,0 +1,76 @@ +cdist-type__pyvenv(7) +================== +Darko Poljak + + +NAME +---- +cdist-type__pyvenv - Create or remove python virtualenv + + +DESCRIPTION +----------- +This cdist type allows you to create or remove python virtualenv. +It assumes pip and virtualenv are already installed. Concrete packages +or installation procedures depend on concrete OS and/or OS +version/distribution. +Ensure this in your init manifest as in the following example: +-------------------------------------------------------------------------------- +case "$__target_host" in + localhost) + __package python3-pip --state present + require="__package/python3-pip" __package_pip virtualenv --pip pip3 --state present + require="__package/python3-pip __package_pip/virtualenv" __pyvenv /home/darko/testenv --owner darko --group darko --mode 740 --state present + ;; +-------------------------------------------------------------------------------- + + +REQUIRED PARAMETERS +------------------- +None + +OPTIONAL PARAMETERS +------------------- +state:: + Either "present" or "absent", defaults to "present" + +group:: + Group to chgrp to. + +mode:: + Unix permissions, suitable for chmod. + +owner:: + User to chown to. + +python:: + Use specific python interpreter for creating virtualenv. + The default is the interpreter that virtualenv was installed with. + +venvparams:: + virtualenv specific parameters to pass to virtualenv invocation. + + +EXAMPLES +-------- + +-------------------------------------------------------------------------------- +__pyvenv /home/services/djangoenv + +# Create python virtualenv for user foo using specific python interpreter. +__pyvenv /home/foo/fooenv --group foo --user foo --python python2.6 + +# Create python virtualenv with specific parameters. +__pyvenv /home/services/djangoenv --venvparams "--relocatable --system-site-packages" +-------------------------------------------------------------------------------- + + +SEE ALSO +-------- +- cdist-type(7) + + +COPYING +------- +Copyright \(C) 2016 Darko Poljak. Free use of this software is +granted under the terms of the GNU General Public License version 3 (GPLv3). diff --git a/cdist/conf/type/__pyvenv/manifest b/cdist/conf/type/__pyvenv/manifest new file mode 100755 index 00000000..bd2b76e9 --- /dev/null +++ b/cdist/conf/type/__pyvenv/manifest @@ -0,0 +1,46 @@ +#!/bin/sh +# +# 2016 Darko Poljak (darko.poljak at gmail.com) +# +# This file is part of cdist. +# +# cdist is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# cdist is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with cdist. If not, see . +# + +# It assumes pip and virtualenv are already installed. Concrete packages +# or installation procedures depend on concrete OS and/or OS +# version/distribution. + +state_should="$(cat "$__object/parameter/state")" +owner="$(cat "$__object/parameter/owner")" +group="$(cat "$__object/parameter/group")" +mode="$(cat "$__object/parameter/mode")" + +case "$state_should" in + present) + : + ;; + + absent) + __directory "$__object_id" --state absent \ + --owner "$owner" \ + --group "$group" \ + --mode "$mode" + ;; + + *) + echo "Unknown state: $state_should" >&2 + exit 1 + ;; +esac diff --git a/cdist/conf/type/__pyvenv/parameter/default/group b/cdist/conf/type/__pyvenv/parameter/default/group new file mode 100755 index 00000000..8b137891 --- /dev/null +++ b/cdist/conf/type/__pyvenv/parameter/default/group @@ -0,0 +1 @@ + diff --git a/cdist/conf/type/__pyvenv/parameter/default/mode b/cdist/conf/type/__pyvenv/parameter/default/mode new file mode 100755 index 00000000..8b137891 --- /dev/null +++ b/cdist/conf/type/__pyvenv/parameter/default/mode @@ -0,0 +1 @@ + diff --git a/cdist/conf/type/__pyvenv/parameter/default/owner b/cdist/conf/type/__pyvenv/parameter/default/owner new file mode 100755 index 00000000..8b137891 --- /dev/null +++ b/cdist/conf/type/__pyvenv/parameter/default/owner @@ -0,0 +1 @@ + diff --git a/cdist/conf/type/__pyvenv/parameter/default/python b/cdist/conf/type/__pyvenv/parameter/default/python new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/cdist/conf/type/__pyvenv/parameter/default/python @@ -0,0 +1 @@ + diff --git a/cdist/conf/type/__pyvenv/parameter/default/state b/cdist/conf/type/__pyvenv/parameter/default/state new file mode 100755 index 00000000..e7f6134f --- /dev/null +++ b/cdist/conf/type/__pyvenv/parameter/default/state @@ -0,0 +1 @@ +present diff --git a/cdist/conf/type/__pyvenv/parameter/default/venvparams b/cdist/conf/type/__pyvenv/parameter/default/venvparams new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/cdist/conf/type/__pyvenv/parameter/default/venvparams @@ -0,0 +1 @@ + diff --git a/cdist/conf/type/__pyvenv/parameter/optional b/cdist/conf/type/__pyvenv/parameter/optional new file mode 100755 index 00000000..6b3fda2e --- /dev/null +++ b/cdist/conf/type/__pyvenv/parameter/optional @@ -0,0 +1,6 @@ +state +group +owner +mode +python +venvparams