# Path: py_mina/subtasks/setup.py
# def create_shared_paths():
#     """
#     Creates shared (+protected) files/dirs and sets unix owner/mode
#     """
# 
#     global shared_path
#     shared_path = fetch('shared_path')
# 
#     def create_dirs(directories, protected=False):
#         global shared_path
# 
#         for sdir in directories:
#             create_entity(
#                 '/'.join([shared_path, sdir]), 
#                 entity_type='directory', 
#                 protected=protected
#             )
# 
# 
#     def create_files(files, protected=False):
#         global shared_path
# 
#         for sfile in files:
#             directory, filename_ = os.path.split(sfile)
# 
#             if directory: 
#                 create_entity(
#                     '/'.join([shared_path, directory]), 
#                     entity_type='directory', 
#                     protected=protected
#                 )
# 
#             filepath = '/'.join([shared_path, sfile])
#             create_entity(filepath, entity_type='file', protected=protected)
# 
#             recommendation_tuple = (env.host_string, filepath)
#             echo_status('\n=====> Don\'t forget to update shared file:\n[%s] %s\n' % recommendation_tuple, error=True)
# 
# 
#     echo_subtask('Creating shared paths')
# 
#     # Shared
#     create_dirs(fetch('shared_dirs', default_value=[]), protected=False)
#     create_files(fetch('shared_files', default_value=[]), protected=False)
# 
#     # Protected
#     create_dirs(fetch('protected_shared_dirs', default_value=[]), protected=True)
#     create_files(fetch('protected_shared_files', default_value=[]), protected=True)
# 
# Path: py_mina/subtasks/deploy.py
# def link_shared_paths():
#     """
#     Links shared paths to build folder
#     """
# 
#     global build_to
#     build_to = fetch('build_to')
# 
#     global shared
#     shared = fetch('shared_path')
# 
#     def link_dirs(dirs):
#         global shared
#         global build_to
# 
#         for sdir in dirs:
#             relative_path = '/'.join(['.', sdir])
#             directory, filename_ = os.path.split(relative_path)
#             shared_path = '/'.join([shared, sdir])
#         
#             with cd(build_to):
#                 create_entity(directory, entity_type='directory', protected=False) # create parent directory
#                 run('rm -rf %s' % relative_path) # remove directory if it conficts with shared
#                 run('ln -s %s %s' % (shared_path, relative_path)) # link shared to current folder
# 
# 
#     def link_files(files):
#         global shared
#         global build_to
#         
#         for sfile in files:
#             relative_path = '/'.join(['.', sfile])
#             directory, filename_ = os.path.split(relative_path)
#             shared_path = '/'.join([shared, sfile])
# 
#             with cd(build_to):
#                 create_entity(directory, entity_type='directory', protected=False) # create parent directory
#                 run('ln -sf %s %s' % (shared_path, relative_path)) # link shared to current folder
# 
# 
#     shared_dirs = fetch('shared_dirs', default_value=[])
#     shared_files = fetch('shared_files', default_value=[])
#     any_shared_dir = len(shared_dirs) > 0
#     any_shared_file = len(shared_files) > 0
# 
#     protected_shared_dirs = fetch('protected_shared_dirs', default_value=[])
#     protected_shared_files = fetch('protected_shared_files', default_value=[])
#     any_protected_shared_dir = len(protected_shared_dirs) > 0
#     any_protected_shared_file = len(protected_shared_files) > 0
# 
#     if any_shared_dir or any_shared_file or any_protected_shared_dir or any_protected_shared_file: 
#         echo_subtask("Linking shared paths")
# 
#         if any_shared_dir: link_dirs(shared_dirs)
#         if any_shared_file: link_files(shared_files)
# 
#         if any_protected_shared_dir: link_dirs(protected_shared_dirs)
#         if any_protected_shared_file: link_files(protected_shared_files)
# 
# def rollback_release():
#     """
#     Rollbacks latest release
#     """
#     
#     ensure('current_path')
# 
#     releases_path = fetch('releases_path')
# 
#     with(settings(show('debug'))):
#         with cd(releases_path):
#             echo_subtask('Finding previous release for rollback')
#             rollback_release_number = run('ls -1A | sort -n | tail -n 2 | head -n 1')
# 
#             if int(rollback_release_number) > 0:
#                 echo_subtask('Linking previous release to current')
#                 run('ln -nfs %s/%s %s' % (releases_path, rollback_release_number, fetch('current_path')))
# 
#                 echo_subtask('Finding latest release')
#                 current_release = run('ls -1A | sort -n | tail -n 1')
# 
#                 if int(current_release) > 0:
#                     echo_subtask('Removing latest release')
#                     run('rm -rf %s/%s' % (releases_path, current_release))
#                 else:
#                     abort('Can\'t find latest release for remove.')
#             else:
#                 abort('Can\'t find previous release for rollback.')
# 
# Path: py_mina/subtasks/git.py
# def git_clone():
#     """
#     Clones repository to tmp build dir
#     """
# 
#     maybe_clone_git_repository()
#     fetch_new_commits()
#     use_git_branch()


from py_mina import *
from py_mina.subtasks import git_clone, create_shared_paths, link_shared_paths, rollback_release


# Settings - shared


set('verbose', True)
set('shared_dirs', ['storage', 'env', 'tmp'])
set('shared_files', ['my_drf_app/db_conf.py', 'my_drf_app/custom_conf.py'])


# Tasks


@task
def restart():
	"""
	Restarts application on remote server
	"""

	# read README.md
	run('sudo monit restart -g my_drf_app_prod')


@deploy_task(on_success=restart)
def deploy():
	"""
	Runs deploy process on remote server
	"""

	git_clone()

# Path: py_mina/subtasks/setup.py
# def create_shared_paths():
#     """
#     Creates shared (+protected) files/dirs and sets unix owner/mode
#     """
# 
#     global shared_path
#     shared_path = fetch('shared_path')
# 
#     def create_dirs(directories, protected=False):
#         global shared_path
# 
#         for sdir in directories:
#             create_entity(
#                 '/'.join([shared_path, sdir]), 
#                 entity_type='directory', 
#                 protected=protected
#             )
# 
# 
#     def create_files(files, protected=False):
#         global shared_path
# 
#         for sfile in files:
#             directory, filename_ = os.path.split(sfile)
# 
#             if directory: 
#                 create_entity(
#                     '/'.join([shared_path, directory]), 
#                     entity_type='directory', 
#                     protected=protected
#                 )
# 
#             filepath = '/'.join([shared_path, sfile])
#             create_entity(filepath, entity_type='file', protected=protected)
# 
#             recommendation_tuple = (env.host_string, filepath)
#             echo_status('\n=====> Don\'t forget to update shared file:\n[%s] %s\n' % recommendation_tuple, error=True)
# 
# 
#     echo_subtask('Creating shared paths')
# 
#     # Shared
#     create_dirs(fetch('shared_dirs', default_value=[]), protected=False)
#     create_files(fetch('shared_files', default_value=[]), protected=False)
# 
#     # Protected
#     create_dirs(fetch('protected_shared_dirs', default_value=[]), protected=True)
#     create_files(fetch('protected_shared_files', default_value=[]), protected=True)
# 
# Path: py_mina/subtasks/deploy.py
# def link_shared_paths():
#     """
#     Links shared paths to build folder
#     """
# 
#     global build_to
#     build_to = fetch('build_to')
# 
#     global shared
#     shared = fetch('shared_path')
# 
#     def link_dirs(dirs):
#         global shared
#         global build_to
# 
#         for sdir in dirs:
#             relative_path = '/'.join(['.', sdir])
#             directory, filename_ = os.path.split(relative_path)
#             shared_path = '/'.join([shared, sdir])
#         
#             with cd(build_to):
#                 create_entity(directory, entity_type='directory', protected=False) # create parent directory
#                 run('rm -rf %s' % relative_path) # remove directory if it conficts with shared
#                 run('ln -s %s %s' % (shared_path, relative_path)) # link shared to current folder
# 
# 
#     def link_files(files):
#         global shared
#         global build_to
#         
#         for sfile in files:
#             relative_path = '/'.join(['.', sfile])
#             directory, filename_ = os.path.split(relative_path)
#             shared_path = '/'.join([shared, sfile])
# 
#             with cd(build_to):
#                 create_entity(directory, entity_type='directory', protected=False) # create parent directory
#                 run('ln -sf %s %s' % (shared_path, relative_path)) # link shared to current folder
# 
# 
#     shared_dirs = fetch('shared_dirs', default_value=[])
#     shared_files = fetch('shared_files', default_value=[])
#     any_shared_dir = len(shared_dirs) > 0
#     any_shared_file = len(shared_files) > 0
# 
#     protected_shared_dirs = fetch('protected_shared_dirs', default_value=[])
#     protected_shared_files = fetch('protected_shared_files', default_value=[])
#     any_protected_shared_dir = len(protected_shared_dirs) > 0
#     any_protected_shared_file = len(protected_shared_files) > 0
# 
#     if any_shared_dir or any_shared_file or any_protected_shared_dir or any_protected_shared_file: 
#         echo_subtask("Linking shared paths")
# 
#         if any_shared_dir: link_dirs(shared_dirs)
#         if any_shared_file: link_files(shared_files)
# 
#         if any_protected_shared_dir: link_dirs(protected_shared_dirs)
#         if any_protected_shared_file: link_files(protected_shared_files)
# 
# def rollback_release():
#     """
#     Rollbacks latest release
#     """
#     
#     ensure('current_path')
# 
#     releases_path = fetch('releases_path')
# 
#     with(settings(show('debug'))):
#         with cd(releases_path):
#             echo_subtask('Finding previous release for rollback')
#             rollback_release_number = run('ls -1A | sort -n | tail -n 2 | head -n 1')
# 
#             if int(rollback_release_number) > 0:
#                 echo_subtask('Linking previous release to current')
#                 run('ln -nfs %s/%s %s' % (releases_path, rollback_release_number, fetch('current_path')))
# 
#                 echo_subtask('Finding latest release')
#                 current_release = run('ls -1A | sort -n | tail -n 1')
# 
#                 if int(current_release) > 0:
#                     echo_subtask('Removing latest release')
#                     run('rm -rf %s/%s' % (releases_path, current_release))
#                 else:
#                     abort('Can\'t find latest release for remove.')
#             else:
#                 abort('Can\'t find previous release for rollback.')
# 
# Path: py_mina/subtasks/git.py
# def git_clone():
#     """
#     Clones repository to tmp build dir
#     """
# 
#     maybe_clone_git_repository()
#     fetch_new_commits()
#     use_git_branch()


# Path: examples/django-rest-framework/deploy.py
from py_mina import *
from py_mina.subtasks import git_clone, create_shared_paths, link_shared_paths, rollback_release



# Settings - shared


set('verbose', True)
set('shared_dirs', ['storage', 'env', 'tmp'])
set('shared_files', ['my_drf_app/db_conf.py', 'my_drf_app/custom_conf.py'])


# Tasks


@task
def restart():
	"""
	Restarts application on remote server
	"""

	# read README.md
	run('sudo monit restart -g my_drf_app_prod')


@deploy_task(on_success=restart)
def deploy():
	"""
	Runs deploy process on remote server
	"""

	git_clone()

	link_shared_paths()