summaryrefslogtreecommitdiff
path: root/bin/cfn-create-aws-symlinks
blob: 3d075db29edd209001a910aa319c48ceb5f2d604 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python
#
#    Licensed 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.

"""
Creates symlinks for the cfn-* scripts in this directory to /opt/aws/bin
"""
import argparse
import glob
import os
import os.path


def create_symlink(source_file, target_file, override=False):
    if os.path.exists(target_file):
        if (override):
            os.remove(target_file)
        else:
            print '%s already exists, will not replace with symlink' % target_file
            return
    print '%s -> %s' % (source_file, target_file)
    os.symlink(source_file, target_file)

def check_dirs(source_dir, target_dir):
    print '%s -> %s' % (source_dir, target_dir)
    
    if source_dir == target_dir:
        print 'Source and target are the same %s' % target_dir
        return False
    
    if not os.path.exists(target_dir):
        try:
            os.makedirs(target_dir)
        except OSError as exc:
            print 'Could not create target directory %s: %s' % (target_dir, exc)
            return False
    return True

def create_symlinks(source_dir, target_dir, glob_pattern, override):
    source_files = glob.glob(os.path.join(source_dir, glob_pattern))
    for source_file in source_files:
        target_file = os.path.join(target_dir, os.path.basename(source_file))
        create_symlink(source_file, target_file, override=override)

if __name__ == '__main__':
    description = 'Creates symlinks for the cfn-* scripts in this directory to /opt/aws/bin'
    parser = argparse.ArgumentParser(description=description)
    parser.add_argument(
        '-t', '--target',
        dest="target_dir",
        help="Target directory to create symlinks",
        default='/opt/aws/bin',
        required=False)
    parser.add_argument(
        '-s', '--source',
        dest="source_dir",
        help="Source directory to create symlinks from. Defaults to the directory where this script is",
        default='/usr/bin',
        required=False)
    parser.add_argument(
        '-f', '--force',
        dest="force",
        action='store_true',
        help="If specified, will create symlinks even if there is already a target file",
        required=False)
    args = parser.parse_args()

    if not check_dirs(args.source_dir, args.target_dir):
        exit(1)

    create_symlinks(args.source_dir, args.target_dir, 'cfn-*', args.force)