Skip to content

Commit

Permalink
Specify an initial working dir for vscreen and vsftp to use
Browse files Browse the repository at this point in the history
If you set the cd parameter, you will get automatically transported into
that directory when you use vscreen or vsftp. Unfortunately there seem
to be technical limitations that are stopping this from working with
vcssh, so patches, fixes and clever workarounds to make this happen are
welcome!
  • Loading branch information
purpleidea committed Sep 25, 2015
1 parent efa5c6d commit ed32d70
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 6 deletions.
6 changes: 6 additions & 0 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,12 @@ Currently undocumented.
####`folder`
Currently undocumented.

####`cd`
If you set this to a string, the `cd` command will run with this arg after
`vscreen` connects to your vm, but before the `screen` command runs. This will
have the effect of putting you in your favourite working directory
automatically. This also works when using `vsftp`, but not `vcssh`.

####`puppet`
Currently undocumented.

Expand Down
1 change: 1 addition & 0 deletions examples/ansible.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
system: ansible
repository: https://github.com/purpleidea/ansible-simple1
directory: ansible-simple1
:cd: ''
:puppet: false
:classes: []
:shell: []
Expand Down
1 change: 1 addition & 0 deletions examples/atomic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
:sync: rsync
:folder: ''
:extern: []
:cd: ''
:puppet: false
:classes: []
:shell: []
Expand Down
1 change: 1 addition & 0 deletions examples/docker-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
system: docker
repository: https://github.com/purpleidea/docker-simple1
directory: docker-simple1
:cd: ''
:puppet: false
:classes: []
:shell: []
Expand Down
1 change: 1 addition & 0 deletions examples/docker-kubernetes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
system: kubernetes
repository: https://github.com/purpleidea/kube-simple1
directory: kube-simple1
:cd: ''
:puppet: false
:classes: []
:shell: []
Expand Down
1 change: 1 addition & 0 deletions examples/docker-subdirs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
system: docker
repository: https://github.com/fedora-cloud/Fedora-Dockerfiles
directory: Fedora-Dockerfiles
:cd: ''
:puppet: false
:classes: []
:shell: []
Expand Down
1 change: 1 addition & 0 deletions examples/kubernetes-ansible.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
system: ansible
repository: https://github.com/eparis/kubernetes-ansible
directory: kubernetes
:cd: ''
:puppet: false
:classes: []
:shell: []
Expand Down
1 change: 1 addition & 0 deletions examples/kubernetes-atomic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
system: kubernetes
repository: https://github.com/purpleidea/kube-simple1
directory: kube-simple1
:cd: ''
:puppet: false
:classes: []
:shell: []
Expand Down
1 change: 1 addition & 0 deletions examples/kubernetes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
system: kubernetes
repository: ~/code/kubernetes-simple2
directory: fakeapp2
:cd: ''
:puppet: false
:classes: []
:shell: []
Expand Down
1 change: 1 addition & 0 deletions examples/puppet-gluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
system: puppet
repository: https://github.com/purpleidea/puppet-shorewall
directory: shorewall
:cd: ''
:puppet: true
:classes:
'::gluster::simple':
Expand Down
1 change: 1 addition & 0 deletions examples/rhel.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
:sync: rsync
:folder: ''
:extern: []
:cd: ''
:puppet: false
:classes: []
:shell: []
Expand Down
1 change: 1 addition & 0 deletions examples/shell.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
system: shell
repository: https://github.com/purpleidea/shell-simple1
directory: shell-simple1
:cd: ''
:puppet: false
:classes: []
:shell:
Expand Down
55 changes: 49 additions & 6 deletions extras/vagrant.bashrc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ function vsftp {
echo 'Vagrant project not found!' 1>&2 && return 2
fi

# if we find the omv.yaml file, it takes precendence for mtime lookups
if [ -e "$pwd/omv.yaml" ]; then
pfile="$pwd/omv.yaml"
elif [ -e "$pwd/Vagrantfile" ]; then
pfile="$pwd/Vagrantfile"
else
echo 'No vagrant definition found!' 1>&2 && return 2
fi

d="$pwd/.ssh"
f="$d/$1.config"
h="$1"
Expand All @@ -50,13 +59,25 @@ function vsftp {
h=${h:$p:$l}
fi

# if mtime of $f is > than 5 minutes (5 * 60 seconds), re-generate...
if [ `date -d "now - $(stat -c '%Y' "$f" 2> /dev/null) seconds" +%s` -gt 300 ]; then
cdfile="$pwd/.vagrant/$h.cd"

# if cd file is missing, or is older than the definition file, re-generate...
# or if mtime of $f is > than 5 minutes (5 * 60 seconds), re-generate...
if [ ! -e "$cdfile" ] || \
[ $(stat -c '%Y' "$pfile" 2> /dev/null) -gt $(stat -c '%Y' "$cdfile" 2> /dev/null) ] || \
[ `date -d "now - $(stat -c '%Y' "$f" 2> /dev/null) seconds" +%s` -gt 300 ]; then
mkdir -p "$d"
# we cache the lookup because this command is slow...
$VAGRANT ssh-config "$h" > "$f" || rm "$f"
fi
[ -e "$f" ] && sftp -F "$f" "$1"

cdstr='' # path to append to end of sftp
cddir="`cat $cdfile 2>/dev/null`"
if [ -e "$cdfile" ] && [ -n "$cddir" ]; then
cdstr=":$cddir"
fi

[ -e "$f" ] && sftp -F "$f" "$1""$cdstr"
}

# vagrant screen
Expand All @@ -77,6 +98,17 @@ function vscreen {
echo 'Vagrant project not found!' 1>&2 && return 2
fi

# if we find the omv.yaml file, it takes precendence for mtime lookups
if [ -e "$pwd/omv.yaml" ]; then
pfile="$pwd/omv.yaml"
elif [ -e "$pwd/Vagrantfile" ]; then
pfile="$pwd/Vagrantfile"
else
echo 'No vagrant definition found!' 1>&2 && return 2
fi

cmd='screen -xRR'

d="$pwd/.ssh"
f="$d/$1.config"
h="$1"
Expand All @@ -87,13 +119,24 @@ function vscreen {
h=${h:$p:$l}
fi

# if mtime of $f is > than 5 minutes (5 * 60 seconds), re-generate...
if [ `date -d "now - $(stat -c '%Y' "$f" 2> /dev/null) seconds" +%s` -gt 300 ]; then
cdfile="$pwd/.vagrant/$h.cd"

# if cd file is missing, or is older than the definition file, re-generate...
# or if mtime of $f is > than 5 minutes (5 * 60 seconds), re-generate...
if [ ! -e "$cdfile" ] || \
[ $(stat -c '%Y' "$pfile" 2> /dev/null) -gt $(stat -c '%Y' "$cdfile" 2> /dev/null) ] || \
[ `date -d "now - $(stat -c '%Y' "$f" 2> /dev/null) seconds" +%s` -gt 300 ]; then
mkdir -p "$d"
# we cache the lookup because this command is slow...
$VAGRANT ssh-config "$h" > "$f" || rm "$f"
fi
[ -e "$f" ] && ssh -t -F "$f" "$1" 'screen -xRR'

cddir="`cat $cdfile 2>/dev/null`"
if [ -e "$cdfile" ] && [ -n "$cddir" ]; then
cmd="cd '$cddir' && $cmd"
fi

[ -e "$f" ] && ssh -t -F "$f" "$1" $cmd
if [ $? -eq 255 ]; then
# you probably want a shorter timeout if you see this often
echo 'Maybe cached connection was stale? Cleaning...'
Expand Down
14 changes: 14 additions & 0 deletions vagrant/Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ boxurlprefix = '' # default url prefix (useful for private boxes)
sync = 'rsync' # default sync type
folder = '' # default folder prefix
extern = [] # default external module definitions
cd = '' # default directory to cd into if using vscreen
puppet = false # default use of puppet or not
classes = [] # default list or hash of classes to include
shell = [] # default list of shell scripts to run
Expand Down Expand Up @@ -205,6 +206,7 @@ if File.exist?(f)
sync = settings[:sync]
folder = settings[:folder] if not(load_folder)
extern = settings[:extern]
cd = settings[:cd]
puppet = settings[:puppet]
classes = settings[:classes]
shell = settings[:shell]
Expand Down Expand Up @@ -310,6 +312,12 @@ while skip < ARGV.length
extern = []
end

elsif ARGV[skip].start_with?(arg='--omv-cd=')
v = ARGV.delete_at(skip).dup
v.slice! arg

cd = v.to_s # set cd arg

elsif ARGV[skip].start_with?(arg='--omv-puppet=')
v = ARGV.delete_at(skip).dup
v.slice! arg
Expand Down Expand Up @@ -560,6 +568,7 @@ settings = {
:sync => sync,
:folder => folder,
:extern => extern,
:cd => cd,
:puppet => puppet,
:classes => classes,
:shell => shell,
Expand Down Expand Up @@ -930,6 +939,7 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
vm_memory = x.fetch(:memory, memory) # get value
vm_disks = x.fetch(:disks, disks) # get value
vm_disksize = x.fetch(:disksize, disksize) # get value
vm_cd = x.fetch(:cd, cd) # get value
vm_docker = x.fetch(:docker, docker) # get value
vm_docker = array_values_to_array_of_hashes(vm_docker)
vm_puppet = x.fetch(:puppet, puppet) # get value
Expand Down Expand Up @@ -1025,6 +1035,10 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
end
end

# store the path to the cd directory for vscreen to see
cd_file = File.join(projectdir, '.vagrant', "#{h}.cd")
File.open(cd_file, 'w') {|cd_handle| cd_handle.write("#{vm_cd}\n")}

# should we clean this puppet client machine?
if puppet and vm_puppet and h != 'puppet' and snoop.include?(h)
cmd = "puppet cert clean #{h}.#{domain}"
Expand Down

0 comments on commit ed32d70

Please sign in to comment.