Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework the packs pack (more packs-related ChatOps!) #2982

Merged
merged 23 commits into from
Nov 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 0 additions & 47 deletions contrib/packs/actions/check_auto_deploy_repo.py

This file was deleted.

18 changes: 0 additions & 18 deletions contrib/packs/actions/check_auto_deploy_repo.yaml

This file was deleted.

45 changes: 0 additions & 45 deletions contrib/packs/actions/deploy.yaml

This file was deleted.

14 changes: 0 additions & 14 deletions contrib/packs/actions/expand_repo_name.yaml

This file was deleted.

11 changes: 11 additions & 0 deletions contrib/packs/actions/get.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
name: "get"
runner_type: "python-script"
description: "Get information about installed pack."
enabled: true
entry_point: "pack_mgmt/get_installed.py"
parameters:
pack:
type: "string"
description: "Name of pack to lookup"
required: true
5 changes: 3 additions & 2 deletions contrib/packs/actions/install.meta.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
---
name: "install"
runner_type: "action-chain"
description: "Installs a pack from StackStorm Exchange into local content repository.
description: "Installs or upgrades a pack into local content repository, either by
git URL or a short name matching an index entry.
Will download pack, load the actions, sensors and rules from the pack.
Note that install require reboot of some st2 services."
Note that install requires reboot of some st2 services."
enabled: true
entry_point: "workflows/install.yaml"
parameters:
Expand Down
91 changes: 91 additions & 0 deletions contrib/packs/actions/pack_mgmt/get_installed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import yaml

from git.repo import Repo
from git.exc import InvalidGitRepositoryError

from st2common.runners.base_action import Action
from st2common.content.utils import get_packs_base_paths
from st2common.constants.pack import MANIFEST_FILE_NAME


class GetInstalled(Action):
""""Get information about installed pack."""
def run(self, pack):
"""
:param pack: Installed Pack Name to get info about
:type pack: ``str``
"""
packs_base_paths = get_packs_base_paths()

pack_path = None
metadata_file = None
for packs_base_path in packs_base_paths:
pack_path = os.path.join(packs_base_path, pack)
pack_yaml_path = os.path.join(pack_path, MANIFEST_FILE_NAME)

if os.path.isfile(pack_yaml_path):
metadata_file = pack_yaml_path
break

# Pack doesn't exist, finish execution normally with empty metadata
if not os.path.isdir(pack_path):
return {
'pack': None,
'git_status': None
}

if not metadata_file:
error = ('Pack "%s" doesn\'t contain pack.yaml file.' % (pack))
raise Exception(error)

try:
details = self._parse_yaml_file(metadata_file)
except Exception as e:
error = ('Pack "%s" doesn\'t contain a valid pack.yaml file: %s' % (pack, str(e)))
raise Exception(error)

try:
repo = Repo(pack_path)
git_status = "Status:\n%s\n\nRemotes:\n%s" % (
repo.git.status().split('\n')[0],
"\n".join([remote.url for remote in repo.remotes])
)

ahead_behind = repo.git.rev_list(
'--left-right', '--count', 'HEAD...origin/master'
).split()
# Dear god.
if ahead_behind != [u'0', u'0']:
git_status += "\n\n"
git_status += "%s commits ahead " if ahead_behind[0] != u'0' else ""
git_status += "and " if u'0' not in ahead_behind else ""
git_status += "%s commits behind " if ahead_behind[1] != u'0' else ""
git_status += "origin/master."
except InvalidGitRepositoryError:
git_status = None

return {
'pack': details,
'git_status': git_status
}

def _parse_yaml_file(self, file_path):
with open(file_path) as data_file:
details = yaml.load(data_file)
return details
31 changes: 8 additions & 23 deletions contrib/packs/actions/expand_repo_name.py → contrib/packs/actions/pack_mgmt/search.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/env python

# Licensed to the StackStorm, Inc ('StackStorm') under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
Expand All @@ -16,27 +14,14 @@
# limitations under the License.

from st2common.runners.base_action import Action
from st2common.services.packs import search_pack_index


class ExpandRepoName(Action):
def run(self, repo_name):
"""Returns the data required to install packs from repo_name.

Keyword arguments:
repo_name -- The Reposistory name to look up in the Packs config.yaml.

Returns: A Dict containing repo_url and subtree.

Raises:
ValueError: If the supplied repo_name is present (or complete).
class PackSearch(Action):
""""Search for packs in StackStorm Exchange and other directories."""
def run(self, query):
"""
# Set up the results object
results = {}

try:
results['repo_url'] = self.config["repositories"][repo_name]["repo"]
results['subtree'] = self.config["repositories"][repo_name]["subtree"]
except KeyError:
raise ValueError("Missing repositories config for '%s'" % repo_name)
else:
return results
:param query: A word or a phrase to search for
:type query: ``str``
"""
return search_pack_index(query)
29 changes: 29 additions & 0 deletions contrib/packs/actions/pack_mgmt/show_remote.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from st2common.runners.base_action import Action
from st2common.services.packs import get_pack_from_index


class ShowRemote(Action):
"""Get detailed information about an available pack from the StackStorm Exchange index"""
def run(self, pack):
"""
:param pack: Pack Name to get info about
:type pack: ``str``
"""
return {
'pack': get_pack_from_index(pack)
}
11 changes: 11 additions & 0 deletions contrib/packs/actions/search.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
name: "search"
runner_type: "python-script"
description: "Search the index for a pack with any attribute matching the query."
enabled: true
entry_point: "pack_mgmt/search.py"
parameters:
query:
type: "string"
description: "A word or a phrase to search for."
required: true
11 changes: 11 additions & 0 deletions contrib/packs/actions/show.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
name: "show"
runner_type: "python-script"
description: "Get detailed information about pack from the remote StackStorm exchange index."
enabled: true
entry_point: "pack_mgmt/show_remote.py"
parameters:
pack:
type: "string"
description: "Name of pack to lookup"
required: true
Loading