Implementing Aptana Studio + Django + Vagrant&Virtual box - django

I set up my virtual environment via vagrant and virtual box. I use Aptana IDE for django development and I'm wondering if there's a way to integrate new projects in aptana with the VM.
I've previously used virtualenv and i just change my python path to include my virtualenv directory. However, with virtual box, i'm not sure how to do that. I thought it'd be the same procedure but i don't think so. With Virtualenv, I was able to locate the projects i created within that directory. When I create a project via Vagrant+Virtual Box, I'm not able to locate the project directory anywhere...it's not in the dedicated directory that i setup for virtual environments. Please help.
Thanks.

you can do this with Vagrant. Let me give you an example:
I. project structure:
/yourdjangoapp
/... # all your app stuff here
/manage.py
/vagrant
/provisioning
/init.sh
/Vagrantfile
/.project
II. /Vagrantfile (simple example w/ default precise64 box):
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "precise64"
config.vm.box_url = "http://files.vagrantup.com/precise64.box"
config.vm.provision :shell, :path => "vagrant/provisioning/init.sh"
config.vm.network :forwarded_port, guest: 8000, host: 8000
end
III. /vagrant/provisioning/init.sh
if [ ! -f /home/vagrant/.vm_initialized ]
then
rm -rf /var/www
ln -fs /vagrant /var/www
touch /home/vagrant/.vm_initialized
fi
What's happening?
Vagrant creates a shared folder for each vm, this defaults to "/vagrant" within the quest os and to the folder where you put your Vagrantfile on the host machine.
we add a provision shell script to our Vagrant config, so Vagrant will run this after booting the VM
in /vagrant/provisioning/init.sh we set a symbolic link for the shared folder to /var/www (just an example, so we can access it with apache w/o further configuration)
as Vagrant will run this on every "vagrant up", we have to check, if the vm is already initialized (if-block in init.sh)
Where to go from here?
Well, you could start your development server with:
python /var/www/yourdjangoapp/manage.py runserver 0.0.0.0:8000
And should be able to access it from your host machine via
http://localhost:8000/
You can edit your project on the host w/ your favorite editor now (placed in /yourdjangoapp).
For a new project create/copy the project structure. As Vagrant creates a new VM for each project, the shared folder in the guest is always linked to your current project's folder.
Working without provisioners
The current example shows the setup I use. My init.sh includes stuff like installing packages, pulling a GIT repo and so on.
Of course you can omit the provising part and work directly with the shared folder available at /vagrant in the guest os. So you should be able to start the development server with:
python /vagrant/yourdjangoapp/manage.py runserver 0.0.0.0:8000
HTH
Christian

Related

Elastic BeanStalk app deploy post hook not executing my command

I recently was able to get my Laravel app deployed using codepipeline on Elastic Beanstalk but ran into a problem. I noticed that my routes where failing because of php.conf Nginx configuration. I had to add a few lines of code to EB's nginx php.conf file to get it to work.
My problem now was that after every deployment, the instance of the application I modified the php.conf file was destroyed and recreated fresh. I wanted a way to dynamically update the file after every successful deployment. I had a version of the file I wanted versioned with my application and so wanted to create a symlink to that file after deployment.
After loads of research, I stumbled on appDeploy Hooks on Elastic Beanstalk that runs post scripts after deployment so did this
files:
"/opt/elasticbeanstalk/hooks/appdeploy/post/91_post_deploy_script.sh":
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
sudo mkdir /var/testing1
sudo ln -sfn /var/www/html/php.conf.example /etc/nginx/conf.d/elasticbeanstalk/php.conf
sudo mkdir /var/testing
sudo nginx -s reload
And this for some reason does not work. The symlink is not created so my routes are still not working..
I even added some mkdir so am sure the commands in that script runs, none of those commands ran because none of those directories where created.
Please note that if I ssh into the ec2 instance and run the commands there it works. That bash script also exists in the post directory and if I manually run in on the server it works too.
Any pointers to how I could fix this would be helpful. Maybe I am doing something wrong too.
Now I have gotten my scripts to run by following this. However, the script is not running. I am getting an error
2020/06/28 08:22:13.653339 [INFO] Following platform hooks will be executed in order: [01_myconf.config]
2020/06/28 08:22:13.653344 [INFO] Running platform hook: .platform/hooks/postdeploy/01_myconf.config
2020/06/28 08:22:13.653516 [ERROR] An error occurred during execution of command [app-deploy] - [RunPostDeployHooks]. Stop running the command. Error: Command .platform/hooks/postdeploy/01_myconf.config failed with error fork/exec .platform/hooks/postdeploy/01_myconf.config: permission denied
I tried to follow this forum post here to make my file executable by adding to my container command a new command like so:
01_chmod1:
command: "chmod +x .platform/hooks/postdeploy/91_post_deploy_script.sh"
I am still running into the same issue. Permission denied
Sadly, the hooks you are describing (i.e. /opt/elasticbeanstalk/hooks/appdeploy) are for Amazon Linux 1.
Since you are using Amazon Linux 2, as clarified in the comments, the hooks you are trying to use do not apply. Thus they are not being executed.
In Amazon Linux 2, there are new hooks as described here and they are:
prebuild – Files here run after the Elastic Beanstalk platform engine downloads and extracts the application source bundle, and before it sets up and configures the application and web server.
predeploy – Files here run after the Elastic Beanstalk platform engine sets up and configures the application and web server, and before it deploys them to their final runtime location.
postdeploy – Files here run after the Elastic Beanstalk platform engine deploys the application and proxy server.
The use of these new hooks is different than in Amazon Linux 1. Thus you have to either move back to Amazon Linux 1 or migrate your application to Amazon Linux 2.
General migration steps from Amazon Linux 1 to Amazon Linux 2 in EB are described here
Create a folder called .platform in your project root folder and create a file with name 00_myconf.config inside the .platform folder.
.platform/
00_myconf.config
Open 00_myconf.config and add the scripts
files:
"/opt/elasticbeanstalk/hooks/appdeploy/post/91_post_deploy_script.sh":
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
sudo mkdir /var/testing1
sudo ln -sfn /var/www/html/php.conf.example /etc/nginx/conf.d/elasticbeanstalk/php.conf
sudo mkdir /var/testing
sudo nginx -s reload
Commit your changes or reupload the project. This .platform folder will be considered in each new instance creation and your application will deploy properly in all the new instances Amazon Elastic beanstalk creates.
If you access the documentation here and scroll to the section with the title "Application example with extensions" you can see an example of the folder structure of your .platform folder so it adds your custom configuration to NGINX conf on every deploy.
You can either replace the entire nginx.conf file with your file or add additional configuration files to the conf.d directory
Replace conf file with your file on app deploy:
.platform/nginx/nginx.conf
Add configuration files to nginx.conf:
.platform/nginx/conf.d/custom.conf

How to create folder on Elastic Beanstalk server to install LetsEncrypt SSL certificate with AcmePHP

I have a site running on an Elastic Beanstalk single instance server and want to add automated SSL certificate generation from LetsEncrypt using the AcmePHP library.
The library tries to store the certificates in ~/.acmephp, which the server responds to with an error
Failed to create "/home/webapp/.acmephp": mkdir(): Permission denied.
The acmephp library doesn't have an option to change the path built in, and rather than fork and recompile the script, I'd like to be able to store the files in the default directory.
Does anyone know how I can give the app permission to create this directory, outside of the web root, or how I can make the server create it automatically and have it be available to the app?
It looks like since it's being ran by the webapp user, when acmePHP is trying to store the certificate under that user's home directory it fails because that directory doesn't exist (afaik the webapp user only runs httpd and it definitely doesn't have a home directory).
A very dirty workaround could be manually creating that file and folder in the . ebextensions folder in your project.The file would be .ebextensions/create_home.config and it would contain something like this:
files:
"/tmp/create-home.sh" :
mode: "000755"
content: |
#!/usr/bin/env bash
mkdir -p /home/webapp
chown webapp:webapp -R /home/webapp
commands:
01_create:
command: "/tmp/create-home.sh"
That script is ran by the root user, and afterwards it changes ownership of the /home/webapp folder to the webapp user and group respectively. Hope it helps

Can't find jetty's root.war

I'm trying to build a docker image with my war file and jetty, and the tutorials seem pretty straght forward except for one thing.
FROM jetty
ADD mysample.war /var/lib/jetty/webapps/root.war
EXPOSE 8080
but I don't have /var/lib/jetty/webapps/root.war on my system. Brew installed jetty into /usr/local/Cellar/jetty/9.4.8.v20171121 but there isn't a root.war under the path.
I'm running macOS 10.12.6 if that matters.
If you are using the official docker image ...
https://hub.docker.com/_/jetty/
.. the /var/lib/jetty path is the ${jetty.base} directory.
When your Dockerfile uses:
ADD mysample.war /var/lib/jetty/webapps/root.war
It is taking your mysample.war and putting it in ${jetty.base}/webapps/ with the special reserved name root.war that uses contextPath = "/".
The locally installed path /usr/local/Cellar/jetty/9.4.8.v20171121 has nothing to do with your docker image, and its likely not a ${jetty.base} directory (it looks like a ${jetty.home} directory path)
If you had used the following instead ...
ADD mysample.war /var/lib/jetty/webapps/hello.war
Then that war would have been deployed to contextPath = "/hello", meaning you would access that via the general url ...
<scheme>://<host:port>/<contextPath>/<resourceInWar>
Examples:
http://localhost:8080/hello/
https://machine.com/hello/main.css
Reference: https://www.eclipse.org/jetty/documentation/9.4.x/automatic-webapp-deployment.html

why do you need to be in the specified directory when creating multi-boxes in vagrant

I'm trying to create multiple-boxes to be loaded by vagrant when writing
vagrant up kali
vagrant up metasploitable2
The Config I have set-up are
Within the Kali VagrantFile
Vagrant.configure("1") do |config|
config.vm.define "kali" do |kali|
kali.vm.box = "Kali"
end
end
Within the Metasploitable2
Vagrant.configure("1") do |config|
config.vm.define "metasploitable2" do |metasploitable2|
metasploitable2.vm.box = "metasploitable2"
end
end
If I browse to the directory where the .vmdk and Vagrantfile are located and say
vagrant up kali
it creates the kali image, however if i'm not in the directory it won't load the VM.
with an error:
The machine with the name 'kali' was not found configured for
this Vagrant environment.
I'm going to presume this is because it's not being able to read the configuration file, but how can i make this globally because I thought you weren't supposed to modify the 'global' vagrantfile at all.
Well, Vagrant has to find the Vagrantfile to read it, doesn't it? =)
So you either have to be in the same directory or any subdirectory below it. Or you can set VAGRANT_CWD environment variable to point to the directory. See the "Lookup Path" section in the Vagrantfile documentation for more information.
You can of course make wrapper script or other shortcuts if you need to use that often.
Btw, you might want to upgrade your Vagrantfiles to use V2 configuration format to use all new features of Vagrant 1.1+.

How did you setup your Django dev environment?

I'm trying to setup a local Django dev environment using VMs enabled with Vagrant but I'm not sure what's the best way to go about it.
I did a git clone for Django files from production server and installed all the modules that the production server has on my local VM. I wanted to avoid installing a database on my local VM but ran into some problems with the sessions. The local machine is using SESSION_COOKIE_DOMAIN='localhost' and the production is using SESSION_COOKIE_DOMAIN='.mydomain.com' so that creates some confusion.
Not to mention that on the setting.py on my dev environment I had to change IPs to point to the public IP address of the database (thus poking a hole on the security) while my production settings.py is using the local IPs so I ended up using different settings.py files.
I can continue experimenting with new methods but I really have to get going with the project and I'm pretty sure some people had this figured out already.
So how did you setup your Django dev environment?
I have a public repo on GitHub available here:
https://github.com/FlipperPA/djangovagrant
Instructions from the README.md:
Django / Python / MySQL
This is a Vagrant project for Django development.
This does not yet support berkshelf or librarian; all necessary repos are included in 'cookbooks'.
Prerequisites, all platforms:
Virtualbox https://www.virtualbox.org/wiki/Downloads
Vagrant http://downloads.vagrantup.com/
Pre-requisites, Windows only:
git-bash
ruby rvm
Fairly easy to get it running:
vagrant up
vagrant ssh djangovm
** (Note: You are now in the Virtualbox VM as superuser vagrant)
sudo apt-get install python-pip
** (Note: PIP is a Python package manager)
sudo apt-get install python-mysqldb
sudo pip install django
Starting a Django project:
django-admin.py startproject django_project
cd django_project
python manage.py runserver [::]:8000
The VM is configured to use port forwarding. If everything went right, you should be able to access the running server through the browser on your computer running the virtual machine at this url:
http://localhost:8001/
New to Django? Next steps? I highly recommend: http://www.tangowithdjango.com/
For more advanced topics, check out Two Scoops of Django: http://twoscoopspress.org/
there are a few django Apps that I've seen to manage this but I always prefer the following in my settings.py as the number of different configs are usually minimal
SITE_TYPE = environ.get( 'SITE_TYPE', 'DEV' )
if SITE_TYPE == 'LIVE':
DEBUG = False
DEFAULT_HOST = ''
else:
DEBUG = True
DEFAULT_HOST = '50.56.82.194'
EMAIL_HOST = DEFAULT_HOST
I can recommend this repository.
You can modify it to support Django projects.
Vagrantfile updates:
config.vm.define "web1", primary: true do |web1_config|
web1_config.ssh.forward_agent = true
# Create a private network, which allows host-only access to the machine
web1_config.vm.network "private_network", ip: "192.168.11.10"
web1_config.vm.hostname = "web1.#{domain}"
web1_config.vm.provision "shell", path: "provisioners/shell/python.setup.sh"
web1_config.vm.provision "shell", path: "provisioners/shell/application.setup.sh"
end
Then add a provisioners/shell/application.setup.sh file with the following content:
#!/bin/bash
local_user=vagrant
if [ ! -n "$(grep "^bitbucket.org " /home/$local_user/.ssh/known_hosts)" ]; then
ssh-keyscan bitbucket.org >> ~/.ssh/known_hosts 2>/dev/null;
fi
if [[ ! -d "/home/$local_user/app" ]]; then
git clone git#bitbucket.org:czerasz/sample-django-app.git /home/$local_user/app
chown -R $local_user:$local_user /home/$local_user/app
su - $local_user -c "source /usr/local/bin/virtualenvwrapper.sh && mkvirtualenv sample-django-app-env && workon sample-django-app-env && pip install -r /home/$local_user/app/requirements.txt"
fi
Change the repository address (git#bitbucket.org:czerasz/sample-django-app.git) and make also sure that you have a requirements.txt in the root of your git repository. Run vagrant up.
Vagrant will start two machines:
web1 with your django project
db1 with a PoestgreSQL database
If you still have issues add the following to your Vagrantfile:
web1_config.ssh.private_key_path = [ '~/.vagrant.d/insecure_private_key', '~/.ssh/bitbucket' ]
And execute this command on your host (the machine where you run vagrant):
ssh-add ~/.ssh/bitbucket
The ~/.ssh/bitbucket is the ssh private key which you use for bitbucket. It can be ~/.ssh/id_rsa or something different depending how you configured it.