3 �~�fY � @ s� d dl Z d dlZd dlZd dlmZmZmZ d dlmZ d dl m Z mZ d dl mZ d dl mZ d dlmZ d dlmZ e je�ZG d d � d �ZG dd� de j�Zd d� Zdd� Zdd� Zee je jffgZdd� ZdS )� N)�gaierror�getaddrinfo� inet_ntoa)�pack)�sources�subp)� url_helper)�util)�dhcp)�ec2c @ s( e Zd ZdZdd� Zdd� Zdd� ZdS ) �CloudStackPasswordServerClienta� Implements password fetching from the CloudStack password server. http://cloudstack-administration.readthedocs.org/ en/latest/templates.html#adding-password-management-to-your-templates has documentation about the system. This implementation is following that found at https://github.com/shankerbalan/cloudstack-scripts/ blob/master/cloud-set-guest-password-debian c C s || _ d S )N)�virtual_router_address)�selfr � r �*/usr/lib/python3.6/DataSourceCloudStack.py�__init__* s z'CloudStackPasswordServerClient.__init__c C s: t j ddddddddd d j|�dj| j�g�\}}|j� S )NZwgetz--quietz--tries�3z --timeoutZ20z--output-document�-z--headerzDomU_Request: {0}z{0}:8080)r �formatr �strip)r Zdomu_request�output�_r r r �_do_request- s z*CloudStackPasswordServerClient._do_requestc C s4 | j d�}|dkrd S |dkr&td��| j d� |S )NZsend_my_password� �saved_passwordZbad_requestz-Error when attempting to fetch root password.)r r )r �RuntimeError)r �passwordr r r �get_passwordB s z+CloudStackPasswordServerClient.get_passwordN)�__name__� __module__�__qualname__�__doc__r r r r r r r r s r c @ sL e Zd ZdZdZdZdd� Zdd� Zdd � Zd d� Z dd � Z edd� �ZdS )�DataSourceCloudStackZ CloudStack�x �2 c C sX t jj| |||� tjj|jd�| _d| _t� | _ | j s@t d��d| j f | _i | _d S )NZcsZlatestzNo virtual router found!z http://%s/) r � DataSourcer �os�path�join�seed_dir�api_ver�get_vr_address�vr_addrr �metadata_address�cfg)r Zsys_cfgZdistro�pathsr r r r T s zDataSourceCloudStack.__init__c C s~ | j � }|jdkrdS tj| jd�g}tj� }tj||j|jtj d�\}}|r\tj d|� ntjd|ttj� | �� t |�S )Nr Fzlatest/meta-data/instance-id)�urlsZmax_waitZtimeoutZ status_cbzUsing metadata source: '%s'z>Giving up on waiting for the metadata from %s after %s seconds)Zget_url_paramsZmax_wait_seconds�uhelpZcombine_urlr- �timeZwait_for_urlZtimeout_seconds�LOGZwarning�debugZcritical�int�bool)r Z url_paramsr0 � start_timeZurlZ _responser r r �wait_for_metadata_service` s$ z.DataSourceCloudStack.wait_for_metadata_servicec C s | j S )N)r. )r r r r �get_config_obj s z#DataSourceCloudStack.get_config_objc C s i }t j|| jd d�r>|d | _|d | _tjd| j� dS y�| j� sLdS tj� }t j | j| j�| _t j | j| j�| _tjdttj� | �� t| j�}y|j� }W n$ tk r� t jtd | j� Y nX |r�d|d did�| _dS tk �r t jtd| j� dS X d S ) N�/)�basez user-dataz meta-dataz%Using seeded cloudstack data from: %sTFz)Crawl of metadata service took %s secondsz/Failed to fetch password from virtual router %sZexpire)Z ssh_pwauthr Zchpasswdz(Failed fetching from metadata service %s)r Zread_optional_seedr) Zuserdata_raw�metadatar3 r4 r8 r2 r Zget_instance_userdatar* r- Zget_instance_metadatar5 r r, r � ExceptionZlogexcr. )r Zseed_retr7 Zpassword_clientZset_passwordr r r � _get_data� sH zDataSourceCloudStack._get_datac C s | j d S )Nzinstance-id)r<