Electric car company Tesla has agreed to buy SolarCity, a leading provider of small-scale solar energy installations for $2. 6 billion.