aboutsummaryrefslogtreecommitdiff
path: root/lib/hydra/misc
blob: b349b40a65dbf501eb39e9093847f6b6d61587e3 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/bin/bash

# Setup main configuration and load preferences
function hydra_config_load {
  local folder="`dirname $CONFIG`"

  if [ -f "$folder" ]; then
    echo "Converting legacy configuration scheme..."
    mv $folder $folder.tmp
    mkdir -p $folder
    mv $folder.tmp $CONFIG
  fi

  if [ ! -e "$CONFIG" ]; then
    echo "Creating $CONFIG..."
    mkdir `dirname $CONFIG`
    touch $CONFIG
    chmod 600 $CONFIG
    echo "# Hydra config file." > $CONFIG
    echo "" >> $CONFIG
  fi

  hydra_config_load_preferences
}

# Load config preferences
function hydra_config_load_preferences {
  # Load custom preferences
  if [ ! -z "$PREFERENCES" ] && [ -f "$PREFERENCES" ]; then
    source $PREFERENCES
  fi
  
  hydra_check_preferences
}

# Check preferences
function hydra_check_preferences {
  if [ ! -z "$PUPPET" ] && [ ! -d "$PUPPET" ]; then
    echo "Puppet folder not found: $PUPPET."
    exit 1
  fi
}

# Load a parameter from config
function hydra_config {
  if [ -z "$CONFIG" ]; then
    echo "Your have to set CONFIG variable in the code"
    exit 1
  elif [ -e "$CONFIG" ]; then
    grep -e "^$1=" $CONFIG | tail -n 1 | cut -d = -f 2 | sed -e 's/"//g' -e "s/'//g" | sed -e 's/ *#.*$//'
  else
    echo "Config file not found: $CONFIG"
    exit 1
  fi
}

# Check if there is a given action
function hydra_has_action {
  if [ -z "$ACTIONS" ]; then
    echo "Your have to set ACTIONS variable in the code"
    exit 1
  fi

  if [ -e "$ACTIONS/$1" ]; then
    true
  else
    false
  fi
}

# Execute an action
function hydra_exec {
  # Setup
  action="$1"
  shift 2
  
  # Dispatch
  if hydra_has_action $action; then
    $ACTIONS/$action $*
  fi
}

# Set needed environment variables and do basic checks.
function hydra_set_env {
  export CONFIG="$HOME/.hydra/config"
  export ACTION="$1"

  if [ ! -z "$HYDRA" ]; then
    export PREFERENCES="$HOME/.hydra/$HYDRA"
  fi
}

# Get a command argument
function hydra_get_command {
  # Aditional parameters
  COMMAND="$1"
  
  if [ -z "$COMMAND" ]; then
    hydra_action_usage command
    exit 1
  fi
}

# Run the action usage
function hydra_action_usage {
  if [ "`type -t "hydra_usage_$BASENAME"`" == "function" ]; then
    # Use custom action usage
    hydra_usage_$BASENAME
  else
    # Default usage
    echo "Usage: $NAME <command> [arguments]"
  fi

  echo "Available commands:"
  ls $ACTIONS | sed -e 's/^/\t/'
}

# Action dispatcher
function hydra_dispatch {
  hydra_exec $ACTION $*
}

# Hydra usage
function hydra_usage_hydra {
  echo "Usage: hydra [hydra] <command> [arguments]"
}

# Hydra ctl usage
function hydra_usage_hydractl {
  echo "Usage: hydractl <command> [arguments]"
}