<commit_before>#!/bin/bash
#
# Obdi - a REST interface and GUI for deploying software
# Copyright (C) 2014  <NAME>
#
# This program 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.
#
# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

export PATH=/bin:/usr/bin

if ! which salt >& /dev/null; then
    echo '{"Error":"Salt binary not found."}'
    exit 1
fi

[[ -z $1 ]] && {
    echo '{ "Error":"Arg1 empty. Expected environment name" }'
    exit 1
}

ENV=$1

# Does the tag/branch exist?

# Get the gitfs remote from the salt master file
gitrepo=`grep -A1 gitfs_remotes: /etc/salt/master | sed -n '${s/^\s*-\s*//p}'`

# Get a list of all the branches/tags
branchtags=`git ls-remote $gitrepo | sed -n '/HEAD/n;s#^.*refs/heads/##p'`

if ! echo "$branchtags" | grep -qs "^$ENV\$" 2>/dev/null; then
    echo '{ "Error":"Branch or tag \"'"$ENV"'\" does not exist." }'
    exit 1
fi

# Fill the salt git cache

# Since 2014.7.0 directory must exist first
mkdir -p /var/cache/salt/master/gitfs/refs/$ENV

output=`python < <(cat <<EnD
# Import python libs
import os
import sys

# Import salt libs
import salt
import salt.cli
import salt.config
import salt.fileserver.gitfs

def salt_update():

    salt.fileserver.gitfs.__opts__ = salt.config.master_config(
            os.environ.get('SALT_MASTER_CONFIG', '/etc/salt/master'))

    pid = os.getpid()
    result = []
    try:
        salt.fileserver.gitfs.update()
        load = {'saltenv':'$ENV'}
        file = salt.fileserver.gitfs.file_list(load)
        for i in file:
            result.append( salt.fileserver.gitfs.find_file(i,'$ENV') )
    except KeyboardInterrupt:
        os.kill(pid, 15)

    return result

print salt_update()
EnD
)`

# Does the env exist now?

# TODO: get this to check for at least one file now we have to create the dir.
[[ ! -d /var/cache/salt/master/gitfs/refs/$ENV ]] && {
    echo '{ "Error":"Unable to fill git cache", "Output":"'"$output"'" }'
    exit 1
}

# Get sls details and output as JSON

cd /var/cache/salt/master/gitfs/refs/; cd ${ENV}
declare -i numstates i<commit_msg>Wrong. The directory does not need to exist first. Reverted commit<commit_after>#!/bin/bash
#
# Obdi - a REST interface and GUI for deploying software
# Copyright (C) 2014  <NAME>
#
# This program 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.
#
# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

export PATH=/bin:/usr/bin

if ! which salt >& /dev/null; then
    echo '{"Error":"Salt binary not found."}'
    exit 1
fi

[[ -z $1 ]] && {
    echo '{ "Error":"Arg1 empty. Expected environment name" }'
    exit 1
}

ENV=$1

# Does the tag/branch exist?

# Get the gitfs remote from the salt master file
gitrepo=`grep -A1 gitfs_remotes: /etc/salt/master | sed -n '${s/^\s*-\s*//p}'`

# Get a list of all the branches/tags
branchtags=`git ls-remote $gitrepo | sed -n '/HEAD/n;s#^.*refs/heads/##p'`

if ! echo "$branchtags" | grep -qs "^$ENV\$" 2>/dev/null; then
    echo '{ "Error":"Branch or tag \"'"$ENV"'\" does not exist." }'
    exit 1
fi

# Fill the salt git cache

output=`python < <(cat <<EnD
# Import python libs
import os
import sys

# Import salt libs
import salt
import salt.cli
import salt.config
import salt.fileserver.gitfs

def salt_update():

    salt.fileserver.gitfs.__opts__ = salt.config.master_config(
            os.environ.get('SALT_MASTER_CONFIG', '/etc/salt/master'))

    pid = os.getpid()
    result = []
    try:
        salt.fileserver.gitfs.update()
        load = {'saltenv':'$ENV'}
        file = salt.fileserver.gitfs.file_list(load)
        for i in file:
            result.append( salt.fileserver.gitfs.find_file(i,'$ENV') )
    except KeyboardInterrupt:
        os.kill(pid, 15)

    return result

print salt_update()
EnD
)`

# Does the env exist now?

[[ ! -d /var/cache/salt/master/gitfs/refs/$ENV ]] && {
    echo '{ "Error":"Unable to fill git cache", "Output":"'"$output"'" }'
    exit 1
}

# Get sls details and output as JSON

cd /var/cache/salt/master/gitfs/refs/; cd ${ENV}
declare -i numstates i
{'fraction_non_alphanumeric': 0.12998610835483232, 'fraction_numerical': 0.007541178805318516, 'mean_word_length': 3.2317380352644838, 'pattern_counts': {'":': 10, '<': 15, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '525691', 'n_tokens_mistral': 1715, 'n_tokens_neox': 1618, 'n_words': 591}