2013-08-07 07:24:10 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2015-03-05 14:02:26 +00:00
|
|
|
# 2013-2015 Nico Schottelius (nico-cdist at schottelius.org)
|
2013-08-07 07:24:10 +00:00
|
|
|
#
|
|
|
|
# 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 <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
import logging
|
2013-08-07 15:52:34 +00:00
|
|
|
import os
|
2016-08-14 20:09:51 +00:00
|
|
|
import tempfile
|
2013-08-07 07:24:10 +00:00
|
|
|
|
2013-08-07 18:58:45 +00:00
|
|
|
# initialise cdist
|
2013-08-18 22:38:19 +00:00
|
|
|
import cdist.exec.local
|
2013-08-07 18:58:45 +00:00
|
|
|
|
|
|
|
|
2013-08-07 15:52:34 +00:00
|
|
|
import cdist.config
|
2013-08-07 07:24:10 +00:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
|
2013-08-07 07:24:10 +00:00
|
|
|
class Shell(object):
|
2016-07-05 18:44:24 +00:00
|
|
|
|
2013-08-07 18:58:45 +00:00
|
|
|
def __init__(self, shell=None):
|
2013-08-07 07:24:10 +00:00
|
|
|
|
2013-08-07 18:58:45 +00:00
|
|
|
self.shell = shell
|
2013-08-07 15:52:34 +00:00
|
|
|
|
2016-08-10 21:56:56 +00:00
|
|
|
self.target_host = (
|
|
|
|
"cdist-shell-no-target-host",
|
|
|
|
"cdist-shell-no-target-host",
|
|
|
|
"cdist-shell-no-target-host",
|
|
|
|
)
|
2017-07-20 20:04:44 +00:00
|
|
|
self.target_host_tags = ""
|
2016-08-10 21:56:56 +00:00
|
|
|
|
2016-08-14 20:09:51 +00:00
|
|
|
host_dir_name = cdist.str_hash(self.target_host[0])
|
|
|
|
base_root_path = tempfile.mkdtemp()
|
|
|
|
host_base_path = os.path.join(base_root_path, host_dir_name)
|
|
|
|
|
2013-09-03 19:32:26 +00:00
|
|
|
self.local = cdist.exec.local.Local(
|
2016-08-14 20:09:51 +00:00
|
|
|
target_host=self.target_host,
|
2017-07-20 20:04:44 +00:00
|
|
|
target_host_tags=self.target_host_tags,
|
2016-08-14 20:09:51 +00:00
|
|
|
base_root_path=host_base_path,
|
|
|
|
host_dir_name=host_dir_name)
|
2013-08-07 15:52:34 +00:00
|
|
|
|
2013-08-07 18:58:45 +00:00
|
|
|
def _init_shell(self):
|
|
|
|
"""Select shell to execute, if not specified by user"""
|
|
|
|
|
|
|
|
if not self.shell:
|
2016-07-05 18:44:24 +00:00
|
|
|
self.shell = os.environ.get('SHELL', "/bin/sh")
|
2013-08-07 18:58:45 +00:00
|
|
|
|
|
|
|
def _init_files_dirs(self):
|
2013-08-18 22:38:19 +00:00
|
|
|
self.local.create_files_dirs()
|
2013-08-07 18:58:45 +00:00
|
|
|
|
|
|
|
def _init_environment(self):
|
|
|
|
self.env = os.environ.copy()
|
2016-07-05 18:44:24 +00:00
|
|
|
additional_env = {
|
2013-08-18 22:38:19 +00:00
|
|
|
'PATH': "%s:%s" % (self.local.bin_path, os.environ['PATH']),
|
2016-07-05 18:44:24 +00:00
|
|
|
# for use in type emulator
|
|
|
|
'__cdist_type_base_path': self.local.type_path,
|
2013-08-07 18:58:45 +00:00
|
|
|
'__cdist_manifest': "cdist shell",
|
2013-09-03 19:32:26 +00:00
|
|
|
'__global': self.local.base_path,
|
2016-08-10 21:56:56 +00:00
|
|
|
'__target_host': self.target_host[0],
|
|
|
|
'__target_hostname': self.target_host[1],
|
|
|
|
'__target_fqdn': self.target_host[2],
|
2013-08-18 22:38:19 +00:00
|
|
|
'__manifest': self.local.manifest_path,
|
|
|
|
'__explorer': self.local.global_explorer_path,
|
2016-06-12 15:55:29 +00:00
|
|
|
'__files': self.local.files_path,
|
2017-07-20 20:04:44 +00:00
|
|
|
'__target_host_tags': self.local.target_host_tags,
|
2013-08-07 18:58:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.env.update(additional_env)
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
self._init_shell()
|
|
|
|
self._init_files_dirs()
|
|
|
|
self._init_environment()
|
2013-08-07 07:24:10 +00:00
|
|
|
|
2017-06-28 21:36:42 +00:00
|
|
|
log.trace("Starting shell...")
|
2016-07-12 18:15:08 +00:00
|
|
|
self.local.run([self.shell], self.env, save_output=False)
|
2017-06-28 21:36:42 +00:00
|
|
|
log.trace("Finished shell.")
|
2013-08-07 18:58:45 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def commandline(cls, args):
|
|
|
|
shell = cls(args.shell)
|
|
|
|
shell.run()
|